2013-03-28 48 views
-3

我已經包含在PHP以下文本的變量:如何在現有的<a href=""></a>上添加target =「_ blank」?

$description = "Flash <a href=\"http://www.aaa.com/\">this coupon</a> <br > <a href="http://www.ggg.com/">Visit here</a> for details <br >"; 

如何添加的

目標= 「_空白」

失蹤的$description變量?

那麼結果將變爲:

$description = "Flash <a href=\"http://www.aaa.com/\" target="_blank">this coupon</a> <br > <a href="http://www.ggg.com/" target="_blank">Visit here</a> for details <br >"; 

我尋找變量$說明已被用戶記錄意外的解決方案。但是我需要系統通過將target =「blank」添加到現有變量中找出並更正它並更新記錄。

+0

你的問題是,爲什麼能」?你是否完全按照你在這裏所描述的那樣去做? – brbcoding

+0

你可以完全按照你所說的去做,但你應該逃避你的引用(甚至在你的第一個例子中),或者考慮用單引號包裝它,併爲HTML使用雙引號物業 –

+0

單一配額在單配額內應該用\逃脫,對於雙配額也是一樣的,你可以在雙配額內使用單配額而不用任何逃生,反之亦然 – 2013-03-28 12:18:09

回答

0

如果你注意的標籤屬性沒有特定的順序,你可以簡單地用<a target=\"_blank\" href

+0

嗨@hexblot,感謝您的評論。實際上,我正在尋找解決方案,即變量$ description是由用戶意外記錄的。但是我需要系統通過將target =「blank」添加到現有變量中找出並更正它,並更新記錄 – Ray

0

你已經做到了更換<a href,但你的代碼將產生錯誤,因爲你沒有逃脫在該"第二個鏈接

$description = "Flash <a href=\"http://www.aaa.com/\" target=\"_blank\">this coupon</a> <br > <a href=\"http://www.ggg.com/\" target=\"_blank\">Visit here</a> for details <br >"; 

或者您可以使用',那麼你就不必逃避

$description = 'Flash <a href="http://www.aaa.com/" target="_blank">this coupon</a> <br > <a href="http://www.ggg.com/" target="_blank">Visit here</a> for details <br >'; 
+0

對不起,我可能誤導了我的問題。剛剛更新了這個問題。 – Ray

0
$description = str_replace('<a href="','<a target="_blank" href="',$description); 
+0

感謝它的工作....非常感謝 – Ray

+1

這不會工作,如果有其他標籤第一次使用,如'' –

7

我不知道你的確切含義。但是,建議在您的HTML中使用'。 。而且,只有「爲回聲 因此,這將是:

$description = "Flash <a href='http://www.aaa.com/' target='_blank'>this coupon</a><br ><a href='http://www.ggg.com/' target='_blank'>Visit here</a> for details <br >"; 

對於添加的目標,你可以使用這個:

$string = preg_replace("/<a(.*?)>/", "<a$1 target=\"_blank\">", $string); 

上找到:preg_replace links in text with <a> with some exception

+0

你應該做的相反。 '''用於字符串,''用於HTML屬性。儘管允許使用單引號,但雙引號對於html來說是「標準」方式。 –

+0

@ eL-Prova感謝它的運作....非常感謝 – Ray

+0

@ user1109161請標記爲答案:) –

相關問題