www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-21-2009, 07:24 PM
    donatello's Avatar
    donatello donatello is offline
    Registered User
     
    Join Date: Jun 2008
    Location: Europe
    Posts: 558
    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 07:26 PM. Reason: English :]
    Reply With Quote
      #2  
    Old 11-21-2009, 07:42 PM
    NogDog's Avatar
    NogDog NogDog is offline
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,007
    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.
    __________________
    "That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! 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." -- from Nation, by Terry Pratchett
    freelancer.internet.com
    Email me
    Reply With Quote
      #3  
    Old 11-21-2009, 07:49 PM
    donatello's Avatar
    donatello donatello is offline
    Registered User
     
    Join Date: Jun 2008
    Location: Europe
    Posts: 558
    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);
    Reply With Quote
      #4  
    Old 11-21-2009, 08:56 PM
    thraddash's Avatar
    thraddash thraddash is offline
    Mega Man X
     
    Join Date: Aug 2009
    Location: South Africa, Midrand
    Posts: 258
    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);
    Reply With Quote
      #5  
    Old 11-22-2009, 10:59 AM
    donatello's Avatar
    donatello donatello is offline
    Registered User
     
    Join Date: Jun 2008
    Location: Europe
    Posts: 558
    [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!
    Reply With Quote
      #6  
    Old 11-22-2009, 11:37 AM
    criterion9 criterion9 is offline
    B.S. CIS
     
    Join Date: Jan 2009
    Posts: 1,354
    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.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:55 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.