Shopping news - Debt Consolidation - Creative Electronics - Debt Consolidation - Indian television shows news

PDA

View Full Version : URL encode on a string that is not just urls?


kemus
Mar 1st 2007, 7:34 pm
Say I have a string:
RANDOM TEXT <a href ="http://google.com/?RANDOM=RANDOM">RANDOM</a>

How can I use the function urlencode on just the
http://google.com/?RANDOM=RANDOM
without urlencoding the rest of the string?

nico_swd
Mar 2nd 2007, 12:14 am
$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;