Find and replace relative url with url from database - PHP
I'm trying to to find internal links in html e.g.
<p>This is first <a href="/old-link-first">old link</a> and this is second
<a href="/old-link-second">old link</a></p>
I have to match relative links make query on database in order to get
links from database, I have problem with matching :/ and executing.
I tried with this but it seems to work only with first link:
$out='<p>This is first <a href="/old-link-first">old link</a> and this is
second <a href="/old-link-second">old link</a></p>';
$pattern = '/href="\/(.*)?"/';
function ch($match){
$oldurl = 'href="'.$match[1].'"';
// query to database
return $newurl;
}
$out = preg_replace_callback($pattern, 'ch', $out);
print $out;
Probably I have lots of mistakes Thanks in advance
No comments:
Post a Comment