Re: Checking equal number of <div> and </div> [message #171298 is a reply to message #171293] |
Thu, 30 December 2010 04:46 |
jwcarlton
Messages: 76 Registered: December 2010
Karma:
|
Member |
|
|
On Dec 29, 9:16 pm, richard <mem...@newsguy.com> wrote:
> On Wed, 29 Dec 2010 17:19:13 -0800 (PST), jwcarlton 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?
>
>> 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);
>> }
>
> a simple way would be to run it through the online validator.
>
> I use Rapid PHP 2010 which shows the matching tags when the cursor is
> between them.
> So if I have <div><div></div> I'll know about it real quick.
I don't think that this would work for this particular application,
but it does look like a killer editor. I used to use HTML Beauty,
which was by far the best I'd had, but it doesn't seem to work too
well with Win7. I'm using Notepad++ now, which is OK, but I miss my
Beauty!
|
|
|