URL encode on a string that is not just urls?

Discussion in 'PHP' started by kemus, Mar 1, 2007.

  1. #1
    Say I have a string:
    How can I use the function urlencode on just the
    without urlencoding the rest of the string?
     
    kemus, Mar 1, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    $text = 'RANDOM TEXT <a href="http://google.com/?RANDOM=RANDOM">RANDOM</a>';
    
    $text = preg_replace('/<a(.+)href\s?=\s?"([^"]+)"([^>]+)>([^<]+)<\/a>/e', "'<a$1href=\"'. urlencode('$2') .'$3>$4</a>'", $text);
    
    echo $text;
    
    
    
    PHP:
     
    nico_swd, Mar 2, 2007 IP