Как удалить все изображения из HTML с DOM
Я пытаюсь удалить все изображения из строки HTML. Я могу удалить только первый, и я не знаю почему.
код:
<?php
$str='<div>
<a href=
"https://www.google.com">
<img src=
"image1.jpg"
alt="image-1.jpg" /></a>
</div>
<p>
hobby\'s vs hobbies
</p>
<div>
<a href=
"https://www.google.com">
<img src=
"image2.jpg"
alt="image-2.jpg" /></a>
</div>';
$dom=new domDocument;
$dom->loadHTML($str);
$images=$dom->getElementsByTagName('img');
foreach($images as $image)
{
$image->parentNode->removeChild($image);
}
$result=$dom->saveHTML();
echo '<textarea>'.$result.'</textarea>';
?>
4 ответа
Решение
Проверьте ответ Марко Гамба
// ...loading the DOM
$dom = new DOMDocument();
@$dom->loadHTML($string); // Using @ to hide any parse warning sometimes resulting from markup errors
$dom->preserveWhiteSpace = false;
// Here we strip all the img tags in the document
$images = $dom->getElementsByTagName('img');
$imgs = array();
foreach($images as $img) {
$imgs[] = $img;
}
foreach($imgs as $img) {
$img->parentNode->removeChild($img);
}
$str = $dom->saveHTML();
Вы также можете сделать это, открыв HTML-файл с помощью file_get_contents('file.html');
и запись в файл с file_put_contents('file.html');
Я использовал следующий пример с пользовательской функцией
//get HTML File
$html_File_With_Images = file_get_contents('file.html_html');
//strip images
$html_file_without_Images = stripImages($html_file_with_images);
//save html file
fopen('file.html', 'W');//open file with write permission
file_put_contents('file.html', $html_file_without_Images);//this writes the contents to file
fclose('file.html');//always close files that you have opened to prevent memory leaks
function stripImages($string)//Recursiveley removes images from an html string
{
$imageStart = strpos($string, "<img");//find "<img" in the html string
$imageSubString = substr($string,$imageStart);//you need to isolate the end of the image, because images do not have end tags
$imageLength = strpos($imageSubString, ">");//find the image end tag, which will be the first > charachter from the start of the tag
$imageEnd = $imageStart + $imageLength + 1;//this integer points to where the image ends (+1 because of 0-indexing)
$returnStart = substr($string,0,$imageStart);//this is the retun string, before the image
$returnEnd = substr($string,$imageEnd);//this is the return string, after the image
$return = $returnStart . $returnEnd;//this appends the $returnStart and $returnEnd strings into one string
$test = strpos($return, "<img");//tests if there are more images in the string
if($test !== false)//must use !== because strpos can return 0 (which looks false) if the searched string is at the start of the string
{
$return = stripImages($return);//this recursiveley runs the function until there are no more images to display
}
return($return);//output
}
Foreach на nodeList не работает должным образом (он получает только первый элемент), вместо этого вы должны зациклить его с индексом
Вы можете сделать это очень легко, если вы используете функцию remove() JQuery.
$("img").remove();
Надеюсь, это поможет.