2012-01-19 127 views
1

我需要一些帮助。 我有一个巨大的PHP文件,其中包含很多链接。 我需要替换# 链接,例如:替换文件中的所有链接

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 ..... 

,所以我只需要改变的环节不出意外,链接文本等应该保持原样。

谢谢您的阅读。

回答

3

当没有其他属性:

$string = preg_replace('~<a href="[^"]+">~', '<a href="#">', $string); 

否则:

$string = preg_replace('~<a ([^>]*)href="[^"]+"([^>]*)>~', '<a \\1href="#"\\2>', $string); 

演示:

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 ..... 
+0

谢谢你了 – JoinOG

0

尝试以下操作:

$str = 'Original link: text....<a href="orig-link"> Link text </a> other text .....'; 
$newstr = preg_replace('/(href=.)[^"]+/', '$1#', $str); 
echo $newstr; 

输出结果为:

Original link: text....<a href="#"> Link text </a> other text .....