PHP Simple DOM Parser

Hi guys, I'm using this wonderful class here to do a little filtering of code inlining: http://simplehtmldom.sourceforge.net/ . It extends the PHP DOM document class.

Pretty much what I am doing is parsing the string through this class containing the embed code, I am grabbing unique bits of information like id, width, height send through a handler function that inserts id, width, height, etc. into my predefined "safe" template and re-insert my safe template instead of the embed code that the user supplied. It might seem like it's being done the other way around, but this is the way it should be done :)

This all works great. The problem is there is more to the line than just embed code, as I can't just replace the embed code. I can only replace the whole line that wipes out the rest of the tags, etc. For example, if there was a p tag that was wiped.

So my question is, how can I just replace a certain part of the string with this class? Spent the last few days trying to figure this out and need some more input. It seems the class can do this, so I'm stumped.

Here is a basic version of what I have so far :)

    // load the class
    $html = new simple_html_dom();

    // load the entire string containing everything user entered here
    $return = $html->load($string);

    // check for embed tags
    if($html->find('embed') == true
    {
         foreach($html->find('embed') as $element)
         {
              // send it off to the function which returns a new safe embed code
              $element = create_new_embed($parameters);

              // this is where i somehow i need to save the changes and send it back to $return
         }
    }

      

Any input would be greatly appreciated. If I have explained my problem well enough please let me know :)

+2


a source to share


1 answer


When you do this:

foreach($html->find('embed') as $element)

      

A simpleHTMLdom element object is created - its not a simple variable that you can simply use as a target. For exmaple, if the element was:

<embed>Some Embed Code Here</embed>

      

You can change it by following these steps:



$element->innertext = "Hello world";
# the element is now <embed>Hello world</embed>

      

if you want to completely change the element:

$element->outertext = "<p>Now I'm a paragraph</p>";

      

and you've completely replaced embed.

+4


a source







All Articles