I am trying to update some URLs in the database.
The following is a part of the subject text that I am working on:
abcd abc(<a href=http://www14.xx.com/banket00/xx.aspx?id=384>abc</a>). abc ab %30-40 abcd. <a href=http://www.xx.edu.it/~xx/xxx.html target=_blank>abc</a> abcd xxx.
(7061, 931, 'xx', 'xx. <a class=''abc'' href=xx.aspx?id=1657 target=right>"xxxx! xxx</a>', 1, NULL, 0, '2005-02-07 17:47:00', 0),
xx <a class=''abc'' href=xx.aspx?id=4340 target=right>xxx</a>''xxx.<br>04 x 2007 : xxxx.com 33000 xxxxxx.', 1, '2007-11-04 18:13:00', 0, '2007-11-04 16:12:00', 0),
(35761, 5257, 'xxxx', '<a href="http://x.org.global-x.com/xxx.aspx?id=5249&jj=1" target=_blank>http://x.org.global-x.com/xxxx.aspx?id=5249&jj=1</a>', 1, '0000-00-00 00:00:00', 0, '2009-03-21 12:08:00', 0),
What I am trying to do is to update the URLs that contain "aspx?id=x" into
"'<a href="#" onclick="func(<id_in_url>)"><text_of_link></a>'
"
I have tried many patterns until now and some of them worked for some of the URLs but the latest form I came up with does not work:
pattern: /<a[ class='*abc'*]* href=[a-zA-Z\.-_\/:'"]*?\?id=(\d+)"*[ target=[right|_blank]]*>([^<]*?)<\/a>/i
replacement: '<a href="#" onclick="func($1)">$2</a>'
Is there any condition that your links must fulfill? If not I think it works:
<a[^>]*aspx\?id=(\d+)[^>]*>([^<]+)</a>
<?php
$string = 'abcd abc(<a href=http://www14.xx.com/banket00/xx.aspx?id=384>abc</a>). abc ab %3';
$pattern = '/<a[^>]+aspx\?id=([0-9]*)[^>]*>([^<]*)<\/a>/i';
$replacement = '<a href="#" onclick="func($1)">$2</a>';
echo preg_replace($pattern, $replacement, $string);
Use:
/<a (?:.*?)\bhref=(?:.*?)\?id=(\d+)\b(?:.*?)>([^<]*?)<\/a>/is