2013-03-28 51 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请标记为答案:) –

相关问题