Welcome to WebmasterWorld Guest from 34.236.171.181

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg_replace and str_replace

     
10:49 pm on May 24, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


Hi all

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

7:10 am on May 25, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 15, 2004
posts:1867
votes: 0


$fish1 ="Fish Chips";
<a href="http;//www.example.com/">$fish1</a>
$fish2 = strtolower(str_replace(" ", "", $fish1));
<a href="http;//www.example.com/{$fish1}">{$fish2}</a>

I hope I understood what is required.

Hab

7:25 am on May 25, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


sort of, but i need to find the links first as its a complete page thats buffered.

Hope that makes sense

Thanks

2:56 pm on May 25, 2006 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


It makes sense, and your first attempt was very close, just some syntax issues. And Habtom's example shows you how to incorporate the str_replace, but we need to get it inside the preg_replace function so that it will work globally and update all the urls as you wish.
$pattern = "/a\s+href\s*=\s*['\"]?([^'\">]+)['\"]?>([^<]*)</ie"; 
$replacement = "'a href=\"$1'.strtolower(str_replace(' ', '', '$2')).'\">$2<'";
$string = preg_replace($pattern, $replacement, $string);

The pattern says to find:
  1. 'a href' -- there must be at least one space in between the 'a' and the 'href' ( \s+ ),
  2. followed by zero or more spaces( \s* ),
  3. followed by the equal sign ( = ),
  4. followed by zero or more spaces ( \s* ),
  5. followed by an optional single quotation mark or double quotation mark ( ['\"]? ),
  6. followed by one or more of anything that is not a single quotation mark, a double quotation mark or an end element marker and capture this subpattern by using parentheses ( ([^'\">]+) ),
  7. followed by an optional closing single quotation mark or double quotation mark ( ['\"]? ),
  8. followed by a closing element marker ( > ),
  9. followed by zero or more of anything that is not an opening element marker ( ([^<]*) ),
  10. followed by an opening element marker.

I added the 'i' modifier to your already existing 'e' modifier to tell it to be case-insensitive as well. You obviously already know the 'e' modifier tells our replacement value to be evaluated as PHP code.

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.

3:36 pm on May 25, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


Many thanks coopster

Thats just what i needed, im pretty good on expressions but when evaluating php in them i get a bit stuck.

Cheers