Replace Link Value PHP
preg_replace_callback -> Perform a regular expression search and replace using a callback
<?php
$string = '<a href='http://abc.com'>Title1</a><a href='http://xyz.com'>Title2</a>';
$pattern = '/<s?ashref[s='']+([^'']+)['']>([^<]+)[^>]+>/';
$result = preg_replace_callback($pattern, 'ReplaceLinkValue', $string);
function ReplaceLinkValue($matches)
{
list($link, $URL, $value) = $matches;
switch ($URL)
{
case 'http://abc.com':
$newValue = 'New Title1';
break;
case 'http://xyz.com':
$newValue = 'New Title2';
break;
default:
return $link;
}
return str_replace($value, $newValue, $link);
}
echo $string;
?>