tags:

views:

37

answers:

1

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

Pretty much what I am doing is parsing a string through this class that contains embed code, i grab the unique bits of information eg id, width, height send through a handler function which inserts the id, width, height etc into my predefined "safe" template and reinsert my safe template in the place of the embed code the user has put in. May seem a backward way of doing it but it's the way it has to be done :)

All of that works fine. Problem is when there is more than just embed code contained in the string, as I can't just replace the embed code i can only replace the entire string which wipes the rest of the tags etc string. For example if there were a p tag that would be wiped.

So my question is how using this class can i just replace the certain part of the string? Spent the last few days trying to work this out and need some more input. It appears the class can do this so i'm stumped.

Here's 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 gratefully appreciated. If i have explained my problem well enough please let me know :)

+1  A: 

When you do this:

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

You're creating a simpleHTMLdom element object - its not a simple variable you can just use as an assignment. For exmaple, if the element was:

<embed>Some Embed Code Here</embed>

You could change it by doing the following:

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

of if you want to change the element completely:

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

and you've replaced the embed entirely.

Erik
Of course, geez that was obvious. Thank you very much.
Junior Coder
Thank you for quick response before as well!
Junior Coder
No problem. If this answers your question, consider clicking the nice check mark which means ... it best answers your question :-) And welcome to Stackoverflow.
Erik