Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Regex to find URLs in <a> tags


Marked - 1:11 pm on Jul 3, 2011 (gmt 0)


Hi all,

I spent like a day working on this. Basically what I am after is regex to find all the href URLs and <a> tags, and if it contains a certain part of a URL, then replace it. It definitely has to pick up both single and double quotes.

To illustrate, take the following example:
<a id='user_link' class='' href="http://mysite.com/forums_real_path/index.php?showuser=1" title='Your Profile'Username &nbsp;<span id='user_link_dd'></span></a>
I want to use regex to change 'forums_real_path' to just 'forums'. But it must be only in <a></a> tags.

Here's the code I was using, and it was working, but for some reason it didn't work on a bunch of new links:
$pattern='/<\s*A\s*HREF=(\'|")(.*?)forums_real_path(.*?)(\'|")\s*>(.*?)<\/A>/i';
$replacement='<a href=$1$2forums$3$4>$5</a>';
$final_string=preg_replace($pattern,$replacement,$string);


Its kinda messy :/

If anyone could write a much better pattern for me, I'd be very grateful :)


Thread source:: http://www.webmasterworld.com/php/4334407.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com