我想使用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 ...
答案 0 :(得分:2)
为什么有 preg_replace 时为什么要使用 str_replace ?
此代码适用于单个域:
$domain = 'somelink.com';
$abcont = preg_replace("/<a href=\"{$domain}.+\">(.+)<\/a>/iUs", "$1", $abcont);
或者如果您想在多个域上执行而不是$ domain变量,我们将使用数组$ domains
$domains = ['somelink.com'];
$abcont = preg_replace("/<a href=\"(" . join("|", $domains) . ").+\">(.+)<\/a>/iUs", "$2", $abcont);