php preg_replace求解
如题,有一段文本
123456<a href="">abcdefg</a>sdfsdafdfabcdffsafd
目标:将第二次出现的abc进行替换,第一次不做操作
菜鸟求解
[解决办法]
$str = '123456<a href="">abcdefg</a>abceeesdfsdafdfabcdffsafdabcdefg';
$str = preg_replace('/(?<=..(?<![^a]>))abc/iU', '---', $str);
echo $str,'<br>';
只适用于有且仅有标签</a>
[解决办法]
<?php
$str = '<p>元<a target="_blank" href="http://www.trjcn.com/news/news--type-list/cid-21/industry_id-14.html">半导体材料</a><a target="_blank" href="http://www.trjcn.com/news/news--type-list/cid-21/industry_id-14.html">半导体</a>制造厂,导体导体导体导体导体<a target="_blank" href="http://www.trjcn.com/news/news--type-list/cid-21/industry_id-14.html">半导体</a></p>';
$str = preg_replace('/(导体)(?!(:?[^<]
[解决办法]
<(?!a)[^>]*>)*<\/a>)/imu', '<a>$1</a>', $str);
echo $str;