FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Checking equal number of <div> and </div>
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Checking equal number of <div> and </div> [message #171366 is a reply to message #171364] Thu, 30 December 2010 21:06 Go to previous messageGo to previous message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma:
Member
On Dec 30, 3:54 pm, Chuck Anderson <cycletour...@invalid.invalid>
wrote:
> lvaro G. Vicario wrote:
>> El 30/12/2010 2:19, jwcarlton escribi /wrote:
>>> Can you guys think of a good way for me to check a string to make sure
>>> there are an equal number of<div (.*)> and</div>? Then, either add
>>> or remove</div> tags as needed to make them match?
>
>> If you don't care about the exact way to fix the mismatched tags, you
>> can just feed the fragment to an instance of DOMDocument and let it
>> repair the HTML for you:
>
>> <?php
>
>> $fragment = '<div style="font-weight: bold">Lorem ipsum <div>dolor sit
>> amet,
>> <strong><em>luptate</strong></em>. Excepteur proident,
>> <div class="foo">sunt in culpa</div> officia est laborum.';
>
>> $dom = new DOMDocument;
>> libxml_use_internal_errors(TRUE);
>> $dom->loadHTML($fragment);
>> libxml_use_internal_errors(FALSE);
>> echo $dom->saveHTML();
>
>> ?>
>
>> This prints:
>
>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>> "http://www.w3.org/TR/REC-html40/loose.dtd">
>> <html><body><div style="font-weight: bold">Lorem ipsum <div>dolor sit
>> amet,
>> <strong><em>luptate</em></strong>. Excepteur proident,
>> <div class="foo">sunt in culpa</div> officia est
>> laborum.</div></div></body></html>
>
>> (There's probably a way to print only the node but I still haven't
>> figured it out.)
>
> I believe this user comment at php.net might have a solution for that.
>
> http://us3.php.net/manual/en/class.domdocument.php#96709
>
> --
> Chuck Anderson Boulder, CO
> cycletourist.com

I haven't tried it yet, but on paper, this SmartDOMDocument looks like
the way to go. It does the same as DOMDocument that Alvaro suggested,
but it works with fragments instead of complete code.

http://beerpla.net/projects/smartdomdocument-a-smarter-php-domdocument-clas s/

Once I get it implemented, I'll post back with a report.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Ignoring Case on directories
Next Topic: Calculate the distance between 2 points
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 08:56:13 GMT 2024

Total time taken to generate the page: 0.07424 seconds