Forum Moderators: coopster
Im trying to lowercase all urls in a string and also remove any spaces plus add the anchor text to the url.
I have managed this which will lowercase the url and anchor but i cant for the life of me get str_replace to work inside it to remove spaces and lowercase the URL only, not anchor text.
$d = preg_replace('/<a href="(.*?)">(.*?)<\\/a>/e','strtolower("<a href=\"$1\">$2</a>")', $d);
As an example i need to convert:
<a href="http;//www.example.com/">Fish Chips</a>
to:
<a href="http;//www.example.com/fishchips">Fish Chips</a>
I have avoided preg_match as there could be many many url's in the string and i dont want an array.
any wizards out there that can help, much appreciated
Thanks
$pattern = "/a\s+href\s*=\s*['\"]?([^'\">]+)['\"]?>([^<]*)</ie";
$replacement = "'a href=\"$1'.strtolower(str_replace(' ', '', '$2')).'\">$2<'";
$string = preg_replace($pattern, $replacement, $string);
The replacement argument then simply puts our string back together using strtolower [php.net] and str_replace [php.net] to make the captured anchor text formatted as desired.