2012-01-19 124 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 .....