我有2个这样的字符串:
$post_des = 'Neque porro quisquam est qui do <a href = "http://example.com/lorem_ipsum"> lorem ipsum </a> quia dolor sit amet, consectetur, adipisci velit...';
$post_detail = '<a href = "http://example.com/lorem_ipsum">lorem ipsum </a> dolor sit amet, consectetur adipiscing elit. Donec quis purus finibus, ultricies diam at, faucibus nunc. Donec ac luctus ipsum,vitae <a href = "http://example.com/vehicula"> vehicula </a> erat. ' ;
并希望删除所有重复的ahref标记,如下所示:
$post_des = 'Neque porro quisquam est qui do <a href = "http://example.com/lorem_ipsum"> lorem ipsum </a> quia dolor sit amet, consectetur, adipisci velit...';
$post_detail = 'lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis purus finibus, ultricies diam at, faucibus nunc. Donec ac luctus ipsum,vitae <a href = "http://example.com/vehicula"> vehicula </a> erat. ' ;
我尝试使用DOM文档和in_array,但是大约有2个标签(来自lorem ipsum,vehicle)。有没有更简单的方法?
答案 0 :(得分:1)
使用preg_match获取所有链接标记的发生并使用它们进行迭代,以便在$ post_detail上删除(替换为空字符串)。 [小提琴] [1]
if(preg_match('/<a.*\>/',$post_des,$link_tags))
{
foreach($link_tags as $link_tag)
{
preg_match('#<a[^>]+>(.+?)</a>#ims', $link_tag,$str);
$post_detail = str_replace($link_tag,@$str[1],$post_detail);
}
}