1

I have an string with some <img> in it.

$string = '  <img src="pic.jpg">  and <img src="pic2.jpg">';
    $doc = new DOMDocument('1.0', 'UTF-8');
        libxml_use_internal_errors(true);
        $doc->loadHTML(mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8'));
        libxml_clear_errors();
        $imgs = $doc->getElementsByTagName('img');
        foreach ($imgs as $img) 
        {
            if($img->getAttribute('src') == 'pic.jpg')
            {
                // I want delete that picture form string
                $img->parentNode->removeChild($img);
            }
                else
                  $img->setAttribute('class', 'image normall');

        }
        $string = $doc->saveHTML();
        echo $string;

In the end of function when I print $string, the target pic has been delete but for other pic, no add any class to them!

but If I remove $img->parentNode->removeChild($img); , the class will add! what's my wrong?

EDIT please check for this sample string:

$string = '  <img src="pic.jpg">  and <img src="pic2.jpg">';
2
  • I edit my code..please just run this code in a php online in web....I can not underestand your mean? which part? I do not want remove anything else...fo other class I want add just a class... Commented Jun 6, 2015 at 11:20
  • I want add class to all pic. the code works. But if runs this condition if($img->getAttribute('src') == 'pic.jpg') .... the code remove that pic...But unable to add class for other pic Commented Jun 6, 2015 at 11:30

2 Answers 2

1

You can delete nodes if you iterate backwards.

Simply change

// Forward iteration
foreach ($imgs as $img) {

to

// Reverse iteration
for($i = $imgs->length; --$i >= 0;) {
     $img = $imgs->item($i);

Ref: http://php.net/manual/en/class.domnodelist.php#83390

Sign up to request clarification or add additional context in comments.

1 Comment

you are right @Drakes and beated me with 10 second on this. voted-up +10. good
0

Finaly I solved this with this change....hope help others...

$string = '  <img src="pic.jpg">  and <img src="pic2.jpg">';
    $doc = new DOMDocument('1.0', 'UTF-8');
        libxml_use_internal_errors(true);
        $doc->loadHTML(mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8'));
        libxml_clear_errors();
        $imgs = $doc->getElementsByTagName('img');
        $imgs1 = $imgs2 = array();
        foreach($imgs as $img) {
            if($img->getAttribute('src') == 'pic.jpg')
            {
                $imgs1[] = $img;
            }
            else
            $imgs2[] = $img;
        }
        foreach($imgs1 as $img) {
            $img->parentNode->removeChild($img);
        }
        foreach ($imgs2 as $img) 
        {

            $img->setAttribute('class', 'image normall');

        }
        $string = $doc->saveHTML();
        echo $string;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.