2015-04-09 1 views
0

Я пытаюсь удалить все изображения из строки HTML. Я могу удалить только первый, и я не знаю почему.Как удалить все изображения из html с dom

код:

<?php 
$str='<div> 
    <a href= 
    "https://www.google.com"> 
    <img src= 
    "image1.jpg" 
    alt="image-1.jpg" /></a> 
</div> 
<p> 
    hobby\'s vs hobbies&nbsp; 
</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>';  
?> 

ответ

2

Проверить Marco Gamba ответ

// ...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(); 
-2

Вы можете сделать это очень легко, если вы используете JQuery «s remove() function.

$("img").remove(); 

Надеюсь, это поможет.

0

Foreach на Nodelist не действует, как ожидалось (он получает только первый элемент), вы должны контур его с индексом вместо

0

Вы также можете сделать это, открыв 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 
    }