How do I convert the simple_html_dom object back to a string?

I used PHP Simple HTML DOM Parser to first convert HTML string to DOM object using str_get_html()

simple_html_dom.php method

$summary = str_get_html($html_string);

      

  • Then I retrieved the object <img>

    from $summary

    on

    foreach ($summary->find('img') as $img) {
        $image = $img;
        break;
    }
    
          

    Now I needed to convert the $ image DOM object back to a string. I used the Object Oriented way mentioned here :

    $image_string = $image->save();
    
          

    I got the error (from the Moodle debugger):

    Fatal error: Call to undefined method simple_html_dom_node :: save () ...

  • So I thought that since I'm working with Moodle it might have something to do with Moodle, so I just made a simple (non-object oriented?) Path from the same tutorial :

    $image_string = $image;
    
          

    Then, just to check / confirm that it was converted to a string, I did:

    echo '$image TYPE: '.gettype($image);
    echo '<br><br>';
    echo '$image_string TYPE: '.gettype($image_string);
    
          

    But this prints:

    $image TYPE: object
    
    $image_string TYPE: object
    
          

So the question is: why ??? Am I doing something wrong?

+3


source to share


3 answers


You just pass it to a string in the usual way:



$image_string = (string)$image

      

+2


source


Use external text

$image_string = $image->outertext();

      

I looked at the code. save return function



$ret = $this->root->innertext();

      

But this is a class method simple_html_dom

. After searching, you will receive an object simple_html_dom_node

. It does not have such a method and does not inherit. But it has text

, innertext

and outertext

.

+2


source


$ image-> text (); it worked for me

+1


source







All Articles