我有一个变量,它在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 >";如何添加
target="_blank“
$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=“空白”并更新记录来查找和更正它。
发布于 2013-03-28 12:20:33
我不知道你到底是什么意思。但是,在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);发现于: with some exception
发布于 2013-03-28 12:19:30
$description = str_replace('<a href="','<a target="_blank" href="',$description);发布于 2013-03-28 12:18:27
您已经这样做了,但是您的代码将产生错误,因为您没有在第二个链接中转义"
$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 >';https://stackoverflow.com/questions/15681532
复制相似问题