0

嗨,我想删除一个 img 标签,它的 src 是使用 preg_replace 的内容中的 url

前任。

    $content = "<center><img src="http://example.net/wp-content/uploads/2012/10/cell-degeneration-contn.jpg" alt="" title="cell-degeneration-contn" width="950" height="272" class="alignnone size-full wp-image-100" /></center><h2>A first-in-class approach to stop & reverse </h2>";

所以输出将是:

    $content="<center></center><h2>A first-in-class approach to stop & reverse </h2>";

但如果可能的话,最好的输出是:

    $content="A first-in-class approach to stop & reverse ";
4

2 回答 2

2

preg_match_all()可以在这里工作,但对于 HTML,它不是最有效的。

$content = '<center><img src="http://example.net/wp-content/uploads/2012/10/cell-degeneration-contn.jpg" alt="" title="cell-degeneration-contn" width="950" height="272" class="alignnone size-full wp-image-100" /></center><h2>A first-in-class approach to stop & reverse </h2>';

preg_match("/<h2>(.*)<\/h2>/",$content,$matches);
$output = $matches[1];
echo $output;

最简单的方法是使用strip_tags()

$output = strip_tags($content);
echo $output;
于 2012-11-02T17:30:38.797 回答
1

你可以这样做:

    $content = "<center><img src='http://example.net/wp-content/uploads/2012/10/cell-degeneration-contn.jpg' alt='' title='cell-degeneration-contn' width='950' height='272' class='alignnone size-full wp-image-100' /></center><h2>A first-in-class approach to stop & reverse </h2>'";
    preg_match("/<h2>(.+)<\/h2>/", $content, $matches);  
    $match = $matches[1];
    echo $match;
于 2012-11-02T17:39:09.953 回答