如何在现有<a href=""> </a>上添加target =“_ blank”?

时间:2013-03-28 12:14:03

标签: php html tags

我有一个在PhP中包含以下文本的变量:

$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 >";

我正在寻找用户意外记录变量$ description的解决方案。但我需要系统通过将target =“blank”添加到现有变量中来找出并更正它。并更新记录。

4 个答案:

答案 0 :(得分:11)

我不知道你到底是什么意思。但是,建议在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

答案 1 :(得分:1)

您已经这样做了,但是您的代码会因为您没有在第二个链接中转义"而产生错误

$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 >';

答案 2 :(得分:0)

如果您注意到代码的属性没有特定订单,则只需将<a href替换为<a target=\"_blank\" href

答案 3 :(得分:0)

$description = str_replace('<a href="','<a target="_blank" href="',$description);
相关问题