开发者

PHP Simple DOM Parser

开发者 https://www.devze.com 2022-12-31 11:03 出处:网络
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.

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
开发者_如何学C         }
    }

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消