Replace special character

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dillipkumar
    New Member
    • Mar 2008
    • 41

    Replace special character

    Can somebody help me,

    Take any string from command line prompt & replace what ever special character will get in that string that should be replaced with \ character.

    Ex:
    Enter any string: xyz/plks/abc.txt/$

    Out put should be like this:
    xyz\/plks\/abc\.txt\/\$

    Thanks
    Dillip
  • cnsabar
    New Member
    • Dec 2007
    • 40

    #2
    Hi.,

    Try this.,
    [code=perl]
    $InputString=~s #(\W)#\\$1#gi;
    [/code]

    Originally posted by dillipkumar
    Can somebody help me,

    Take any string from command line prompt & replace what ever special character will get in that string that should be replaced with \ character.

    Ex:
    Enter any string: xyz/plks/abc.txt/$

    Out put should be like this:
    xyz\/plks\/abc\.txt\/\$

    Thanks
    Dillip

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      or try the quotemeta() function:

      Code:
      $foo = 'xyz/.?.!@#$%^&*()_+/abc';
      $foo = quotemeta($foo);
      print $foo;

      Comment

      • dillipkumar
        New Member
        • Mar 2008
        • 41

        #4
        Originally posted by cnsabar
        Hi.,

        Try this.,
        [code=perl]
        $InputString=~s #(\W)#\\$1#gi;
        [/code]

        Thanks~
        Now it is working fine.

        Comment

        Working...