Welcome to WebmasterWorld Guest from 54.146.246.4

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regex to find URLs in <a> tags

     

Marked

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

5+ Year Member



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 :)

Software error:

Can't locate /home/deploy/webmasterworld/code_format-v6.lib in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at decode-post-v6.lib line 27, <THREADDAT> line 3.

For help, please send mail to the webmaster (it@imninjas.com), giving this error message and the time and date of the error.