i need some help. i have a huge php document that contain a lot of links inside it. I need to replace links with # for example:
Original link: text....<a href="orig-link"> Link text </a> other text .....
How i need it be: text....<a href="#"> Link text </a> other text .....
so i need to change only the link nothing else, the link text etc should stay as it is.
Thank you for reading.
When there are no other attributes:
$string = preg_replace('~<a href="[^"]+">~', '<a href="#">', $string);
Otherwise:
$string = preg_replace('~<a ([^>]*)href="[^"]+"([^>]*)>~', '<a \\1href="#"\\2>', $string);
Demo:
php > $string = 'text....<a asd="blub" href="orig-link" title="bla"> Link text </a> other text .....';
php > echo preg_replace('~<a ([^>]*)href="[^"]+"([^>]*)>~', '<a \\1href="#"\\2>', $string);
text....<a asd="blub" href="#" title="bla"> Link text </a> other text .....
Try the following:
$str = 'Original link: text....<a href="orig-link"> Link text </a> other text .....';
$newstr = preg_replace('/(href=.)[^"]+/', '$1#', $str);
echo $newstr;
The output is:
Original link: text....<a href="#"> Link text </a> other text .....