Say I have a string: How can I use the function urlencode on just the without urlencoding the rest of the string?
$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: