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 #171343 is a reply to message #171283] Thu, 30 December 2010 14:33 Go to previous messageGo to previous message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma:
Senior Member
In news:a4d4b5ce-6bcb-40eb-9641-27f48ad3229a(at)l17g2000yqe(dot)googlegroups(dot)com,
jwcarlton <jwcarlton(at)gmail(dot)com> typed:
> 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?
>
> I'm sure that I could do something like this, but PHP is
> usually pretty insightful so I thought there might be a
> better way:
>
> // typed up for the example; obviously not in use or tested
> $num_opentag = substr_count($text, '<div');
> $num_closetag = substr_count($text, '</div>');
>
> // More closed than open
> if ($num_closetag > $num_opentag) {
> $difference = $num_closetag - $num_opentag;
> $text = preg_replace("/<\/div/i", "", $text, $difference);
> }
>
> // More open than closed
> if ($num_opentag > $num_closetag) {
> $difference = $num_opentag - $num_closetag;
> $text = preg_replace("/<div (.*)>/i", "", $text,
> $difference); }

Every time you type a <div>, also go down a couple lines and do the /div.
Then put the "stuff" that goes in between. Works 100% of the time for me!
Another thing you can do is number the div's with comments.

HTH,

Twayne`
[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 10:47:18 GMT 2024

Total time taken to generate the page: 0.07743 seconds