如何将str_replace的标签 删除到特定域?

问题描述 投票:0回答:1

我想使用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 ... 
php str-replace
1个回答
2
投票

嗯,为什么有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);
© www.soinside.com 2019 - 2024. All rights reserved.