Results 1 to 4 of 4

 

Thread: PHP - String replacement

  1. #1
    Member

    Status
    Offline
    Join Date
    Jan 2004
    Location
    UK
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts


    I have the following code:

    echo $ref_url.'<br>';
    echo $keywords[1];

    if (isset($keywords[1]) and ($keywords[1]!="") and ($keywords[1]!=" "))
    {
    $keywords1=preg_replace("/+/"," ",$keywords[1]);
    }

    echo $keywords;

    Which produces the following:

    http://localhost/hvi/viewvilla.php?h...lla+rome&meta=
    holiday+villa+rome
    Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in c:\program files\apache group\apache\htdocs\hvi\logger.inc on line 91

    After - holiday+villa+rome

    Any idea why my +'s aren't changing to spaces??

    NM.
    Holiday Villa In<br>
    <font size=2>If it moves, kick it. If it doesn't move, kick it till it moves and hen kick it because it moved</font>

  2. #2
    straight talker

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    235
    Thanks
    2
    Thanked 0 Times in 0 Posts
    try changing the plusses to %20 thats code for space in url strings

  3. #3
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    2,448
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A + is a special character in regular expressions. You should escape it with a \, like: -
    $keywords1=preg_replace("/\+/"," ",$keywords[1]);

    or, more efficiently, avoid the regular expression and use str_replace: -
    $keywords1=preg_replace('+',' ',$keywords[1]);

    or, assuming you might end up with other encoded characters in your query, you could use: -
    $keywords1=urldecode($keywords[1]);

    By the way, it's also more efficient to use single quotes rather than double quotes if you don't need to substitute variables inside the string, though it is minute differences.

  4. #4
    Member

    Status
    Offline
    Join Date
    Jan 2004
    Location
    UK
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for all the comments, worked a treat!

    NM.

    P.S. I also use single quote normally where possible but they always creep in places
    Holiday Villa In<br>
    <font size=2>If it moves, kick it. If it doesn't move, kick it till it moves and hen kick it because it moved</font>



Thread Information

Users Browsing this Thread

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

Similar Threads

  1. PHP String Function Remove Last X Chr of String
    By scifind in forum Programming
    Replies: 3
    Last Post: 08-07-05, 08:42 AM
  2. Anyone anygood with xml, php, mysql and Affiliate Window
    By AnnonnyMouse in forum Affiliate Marketing Lounge
    Replies: 1
    Last Post: 23-09-04, 11:00 AM
  3. php: find string length
    By morleymouse in forum Programming
    Replies: 2
    Last Post: 25-06-04, 11:47 AM
  4. how to shorten a string (php)
    By morleymouse in forum Programming
    Replies: 3
    Last Post: 27-03-04, 04:35 PM
  5. Removing forward slashes / from a string in PHP
    By giveasyouget in forum Programming
    Replies: 6
    Last Post: 19-09-03, 11:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2