www.webdeveloper.com
Results 1 to 6 of 6

Thread: Escaping a plus sign (+)

  1. #1
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,096

    Escaping a plus sign (+)

    How does one escape a plus sign in PHP, or rather get it to work in the following script:

    PHP Code:
    $who preg_replace('/\%20/'' '$who);
    $who preg_replace('/-/'' '$who); 
    $who preg_replace('/+/'' '$who); 
    The first two expressions work fine, the last is crashing, so there must be a problem with the + being executed instead of parsed as text the way I want... I've tried a backslash to no effect... (I'm not very good at PHP )

    I'm trying to capture an URL which includes plus signs:
    http:// www.URL . com/file.php?dog+cat+mouse

    I'm stripping out the dog+cat+mouse and then stripping the plus signs, replacing them with spaces.
    Last edited by donatello; 11-21-2009 at 06:26 PM. Reason: English :]

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    You could use urldecode() on the string (it should take care of the "+" and any "%xx" characters), then apply any other changes you need to the result.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,096
    Quote Originally Posted by NogDog View Post
    You could use urldecode() on the string (it should take care of the "+" and any "%xx" characters), then apply any other changes you need to the result.
    Thanks!

    It works like a charm:
    PHP Code:
    $who urldecode($who); 
    $who preg_replace('/-/'' '$who); 

  4. #4
    Join Date
    Aug 2009
    Posts
    593
    You should really be using the str_replace function instead, as it seems you don't need a regular expression to do the match.

    PHP Code:
    $who str_replace('-'' '$who); 

  5. #5
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,096

    [resolved]

    Quote Originally Posted by thraddash View Post
    You should really be using the str_replace function instead, as it seems you don't need a regular expression to do the match.

    PHP Code:
    $who str_replace('-'' '$who); 
    Thanks for the tip, done!<P>
    Maybe one of these days I'll know what I'm doing in PHP...

    Can anyone explain why? I'm curious to know...
    is preg_replace more taxing on Apache?
    is preg_replace being deprecated?

    or is it just good programming?

    Thanks!

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    str_replace has less overhead and is less taxing on the processor especially when you are just replacing a single character. More complicated replaces/searches can justify the extra overhead of the ereg/preg functions.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles