根据 src url 从字符串中删除图像

Remove image from string based on src url

我正在寻找一种简单有效的方法来从文章中删除特定图像。我所知道的只是我需要删除的图像的图像 URL。

我的选择是正则表达式或 DOMDocument, probably using an HTML5 parser like https://github.com/Masterminds/html5-php

我的正则表达式技能不太好,我不确定使用正则表达式来完成此操作是否是个好主意,因为我读到应该避免使用正则表达式来解析 HTML。 到目前为止,我对正则表达式的看法是删除完整的图像,但不确定如何根据特定的 src url.

删除它
$img_src = 'http://www.example.org/image_to_be_removed.jpg';

$article = '<h1>Test article with HTML5 tags</h1>
<nav><a href="/link1/">Link 1</a></nav>
<p>This is an example article. The article may or may not include html5 tags, images and other things.</p>
<img src="http://www.example.org/image_to_be_removed.jpg">
<p>More example text.</p>';

$article = preg_replace("/<img[^>]+\>/i", "", $article);
echo $article;

我还没有深入研究 DOMDocument 解决方案,因为我不确定它是否可行,或者正则表达式是否被认为是最佳实践?

你可以试试这个。好像测试没问题无论如何,它应该让您知道如何进行。

$img_src = 'http://www.example.org/image_to_be_removed.jpg';

$article = '<h1>Test article with HTML5 tags</h1>
<nav><a href="/link1/">Link 1</a></nav>
<p>This is an example article. The article may or may not include html5 tags, images and other things.</p>
<img style="width:100px;" src="http://www.example.org/image_to_be_removed.jpg" class="myClass">
<p>More example text.</p>';

$article = preg_replace('/\s{1,}/', ' ', $article);  //Very important step to make sure only 1 space exist between any character.
$img_src = preg_replace('/\//', '\/', $img_src); //Adds slashes to the url.
$regex = '/<img[\W\D\w]{0,}src=\"' . $img_src . '\"[\W\D\w]{0,}>\s/'; //Define the regex.
$article = preg_replace($regex, '', $article);
echo $article;

使用preg_quote:

$article = preg_replace("/<img[^>]+src=\"" . preg_quote($img_src, '/') . "\"[^>]*\>/i", "", $article);

Regex Demo

php Demo

您可以尝试以下 str_replace

<?php
$img_src = 'http://www.example.org/image_to_be_removed.jpg';

$article = '<h1>Test article with HTML5 tags</h1>
<nav><a href="/link1/">Link 1</a></nav>
<p>This is an example article. The article may or may not include html5 tags, images and other things.</p>
<img src="http://www.example.org/image_to_be_removed.jpg">
<p>More example text.</p>';
$new = str_replace('src="http://www.example.org/image_to_be_removed.jpg"','',$article);
echo $article;
echo '<br/>';
echo $new;
?>

您的代码中有 preg_replace 和 str_replace,注意尊重。 还有其他函数可以做同样的事情,比如 sprintf、strtr、str_replace 和 preg_replace 你可以使用任何套件

用正则解析html是not recommended

如您所建议,您可以使用 DOMDocument or for example PHP Simple HTML DOM Parser.

因为您声明 "All that I know is the image URL of the image that I need to remove",您可以使用 xpath 或查找标签名称找到 img 标签的 src 属性并检查。

示例DOM文档:

$img_src = 'http://www.example.org/image_to_be_removed.jpg';
$article = '<h1>Test article with HTML5 tags</h1>
<nav><a href="/link1/">Link 1</a></nav>
<p>This is an example article. The article may or may not include html5 tags, images and other things.</p><img src="http://www.example.org/image_to_be_removed.jpg"><img src="http://www.example.org/image_not_to_be_removed.jpg"><p>More example text.</p>\';
<p>More example text.</p>';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($article);
$xpath = new DOMXPath($dom);
$elements = $xpath->query("//img");
foreach ($elements as $elememnt) {
    if ($elememnt->getAttribute("src") === $img_src) {
        $elememnt->parentNode->removeChild($elememnt);
    }
}
echo $dom->saveHTML();

示例 PHP 简单 HTML DOM 解析器使用 simple_html_dom.php:

$htmlDom = str_get_html($article);
foreach($htmlDom ->find('img[src=http://www.example.org/image_to_be_removed.jpg]') as $item) {
    $item->outertext = '';
}
$htmlDom->save();
echo $htmlDom;