我想使用str_replace从特定域中删除标记
<a href="google.com/1235"> content1
</a>
<a href="somelink.com/2455"> content12
</a>
<a href="google2.com/3"> content13
</a>
<a href="some.com/34858"> content14
</a>
<a href="somelink.com/3"> content14
</a>
<a href="somelink.com/31111"> content14
</a>
<a href="somelink.com/3111d1"> content16
</a> ........ ect ...
在这里,我想删除域中的标记:somelink.com
这就是我所拥有的:
$abcont = file_get_contents ("http://www.example.com");
preg_match_all ('{<a href=somelink.com/.*?> (. *?) </a>}', $abcont, $allLinksMatchs);
$abcont = str_replace ("<a href =", $allLinksMatchs, $abcont);
事实证明:
<a href="google.com/1235"> content1
</a>
content12
<a href="google2.com/3"> content13
</a>
<a href="some.com/34858"> content14
</a>
content14
content14
content16
....... ect ...
嗯,为什么有preg_replace时使用str_replace?
此代码适用于单个域:
$domain = 'somelink.com';
$abcont = preg_replace("/<a href=\"{$domain}.+\">(.+)<\/a>/iUs", "$1", $abcont);
或者如果你想在多个域而不是$ domain变量上做,我们将使用array $ domains
$domains = ['somelink.com'];
$abcont = preg_replace("/<a href=\"(" . join("|", $domains) . ").+\">(.+)<\/a>/iUs", "$2", $abcont);