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
Switch to threaded view of this topic Create a new topic Submit Reply
Checking equal number of <div> and </div> [message #171283] Thu, 30 December 2010 01:19 Go to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
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);
}
Re: Checking equal number of <div> and </div> [message #171292 is a reply to message #171283] Thu, 30 December 2010 01:28 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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?

A recursive RegEx to parse HTML has been posted (by me) and can be easily
found in the manual; whatever is not matched by it can be analyzed further.

However, you should (also) describe the problem you want to solve, not
(only) the problem with your approach for a solution, for the approach might
be completely wrong.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Re: Checking equal number of <div> and </div> [message #171293 is a reply to message #171283] Thu, 30 December 2010 02:16 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
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.
Re: Checking equal number of <div> and </div> [message #171297 is a reply to message #171292] Thu, 30 December 2010 04:42 Go to previous messageGo to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
On Dec 29, 8:28 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> 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?
>
> A recursive RegEx to parse HTML has been posted (by me) and can be easily
> found in the manual; whatever is not matched by it can be analyzed further.
>
> However, you should (also) describe the problem you want to solve, not
> (only) the problem with your approach for a solution, for the approach might
> be completely wrong.
>
> PointedEars
> --
> var bugRiddenCrashPronePieceOfJunk = (
>     navigator.userAgent.indexOf('MSIE 5') != -1
>     && navigator.userAgent.indexOf('Mac') != -1
> )  // Plone, register_function.js:16


It's for a message board, where I've implemented Rich HTML posting
(contentEditable). It works fine except for when someone copy-and-
pastes from a site that uses DIV tags for layout. In those cases, if
the poster doesn't grab the entire page, then it's not uncommon for
the opening or closing DIV tags to be missing.

I've been rebuilding this section using DIV instead of tables, but
those missing tags in the posts cause major layout errors. I can
correct it by surrounding posts with a table, but I figure that a
better long-term solution would be to correct the missing tags as soon
as they're posted.
Re: Checking equal number of <div> and </div> [message #171298 is a reply to message #171293] Thu, 30 December 2010 04:46 Go to previous messageGo to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
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!
Re: Checking equal number of <div> and </div> [message #171302 is a reply to message #171283] Thu, 30 December 2010 04:59 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/29/2010 8:19 PM, 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);
> }

Get an editor that is html aware. It makes your life much simpler.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Checking equal number of <div> and </div> [message #171303 is a reply to message #171297] Thu, 30 December 2010 05:15 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
On Wed, 29 Dec 2010 20:42:41 -0800 (PST), jwcarlton wrote:

> On Dec 29, 8:28 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
> wrote:
>> 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?
>>
>> A recursive RegEx to parse HTML has been posted (by me) and can be easily
>> found in the manual; whatever is not matched by it can be analyzed further.
>>
>> However, you should (also) describe the problem you want to solve, not
>> (only) the problem with your approach for a solution, for the approach might
>> be completely wrong.
>>
>> PointedEars
>> --
>> var bugRiddenCrashPronePieceOfJunk = (
>>     navigator.userAgent.indexOf('MSIE 5') != -1
>>     && navigator.userAgent.indexOf('Mac') != -1
>> )  // Plone, register_function.js:16
>
>
> It's for a message board, where I've implemented Rich HTML posting
> (contentEditable). It works fine except for when someone copy-and-
> pastes from a site that uses DIV tags for layout. In those cases, if
> the poster doesn't grab the entire page, then it's not uncommon for
> the opening or closing DIV tags to be missing.
>
> I've been rebuilding this section using DIV instead of tables, but
> those missing tags in the posts cause major layout errors. I can
> correct it by surrounding posts with a table, but I figure that a
> better long-term solution would be to correct the missing tags as soon
> as they're posted.

In that case, I'd say to have a statement about this in your "new thread"
item. But do you want to see the code for that page or the output?
Most boards only post the code, not the output.
Re: Checking equal number of <div> and </div> [message #171304 is a reply to message #171302] Thu, 30 December 2010 08:37 Go to previous messageGo to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
On Dec 29, 11:59 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 12/29/2010 8:19 PM, 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);
>> }
>
> Get an editor that is html aware.  It makes your life much simpler.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

No, I mean code submitted by the site visitor, not my own code.
Re: Checking equal number of <div> and </div> [message #171307 is a reply to message #171283] Thu, 30 December 2010 09:04 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
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.)


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Checking equal number of <div> and </div> [message #171308 is a reply to message #171304] Thu, 30 December 2010 09:16 Go to previous messageGo to next message
RMP is currently offline  RMP
Messages: 6
Registered: December 2010
Karma: 0
Junior Member
Can you use DOMXPath::query and run "/descendant-or-self::div" on it?

Or DOMXPath::evaluate with "count(/descendant-or-self::div)" ?

Dunno if it works.
Re: Checking equal number of <div> and </div> [message #171315 is a reply to message #171308] Thu, 30 December 2010 09:34 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 30/12/2010 10:16, RMP escribió/wrote:
> Can you use DOMXPath::query and run "/descendant-or-self::div" on it?
>
> Or DOMXPath::evaluate with "count(/descendant-or-self::div)" ?
>
> Dunno if it works.

Excuse the off-topic but these two messages from you have shown up in
the group in the middle of nowhere, starting new threads and containing
no quoted text, so it's difficult to know what you are answering to.

I assume it is because you are using Google Groups, which is possibly
the worse Usenet client even written (even worse than Outlook Express).
If you are interested in the group I suggest you open a Usenet account
at Eternal September <http://www.eternal-september.org/> or a similar
service and connect to it using a specific tool, such as Thunderbird,
Opera, Forte Agent...


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Checking equal number of <div> and </div> [message #171322 is a reply to message #171283] Thu, 30 December 2010 10:53 Go to previous messageGo to next message
RMP is currently offline  RMP
Messages: 6
Registered: December 2010
Karma: 0
Junior Member
Am 30.12.2010 02:19, schrieb jwcarlton:
> 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);
> }

Can you use DOMXPath::query and run "/descendant-or-self::div" on it?

Or DOMXPath::evaluate with "count(/descendant-or-self::div)" ?

Dunno if it works.


--

And sorry folks for the standalone thread! That was because of Googlegroups.
Re: Checking equal number of <div> and </div> [message #171324 is a reply to message #171315] Thu, 30 December 2010 10:56 Go to previous messageGo to next message
RMP is currently offline  RMP
Messages: 6
Registered: December 2010
Karma: 0
Junior Member
Am 30.12.2010 10:34, schrieb "Álvaro G. Vicario":
> El 30/12/2010 10:16, RMP escribió/wrote:
>> Can you use DOMXPath::query and run "/descendant-or-self::div" on it?
>>
>> Or DOMXPath::evaluate with "count(/descendant-or-self::div)" ?
>>
>> Dunno if it works.
>
> Excuse the off-topic but these two messages from you have shown up in
> the group in the middle of nowhere, starting new threads and containing
> no quoted text, so it's difficult to know what you are answering to.
>
> I assume it is because you are using Google Groups, which is possibly
> the worse Usenet client even written (even worse than Outlook Express).
> If you are interested in the group I suggest you open a Usenet account
> at Eternal September <http://www.eternal-september.org/> or a similar
> service and connect to it using a specific tool, such as Thunderbird,
> Opera, Forte Agent...
>
>

Sorry for that. Now I use Thunderbird and aioe. Didn't get eternal
september running yet. Just get no list of groups and try to figure out why.


(will delete this thread soon)
Re: Checking equal number of <div> and </div> [message #171325 is a reply to message #171324] Thu, 30 December 2010 10:59 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 30/12/2010 11:56, RMP escribió/wrote:
> Am 30.12.2010 10:34, schrieb "Álvaro G. Vicario":
>> El 30/12/2010 10:16, RMP escribió/wrote:
>>> Can you use DOMXPath::query and run "/descendant-or-self::div" on it?
>>>
>>> Or DOMXPath::evaluate with "count(/descendant-or-self::div)" ?
>>>
>>> Dunno if it works.
>>
>> Excuse the off-topic but these two messages from you have shown up in
>> the group in the middle of nowhere, starting new threads and containing
>> no quoted text, so it's difficult to know what you are answering to.
>>
>> I assume it is because you are using Google Groups, which is possibly
>> the worse Usenet client even written (even worse than Outlook Express).
>> If you are interested in the group I suggest you open a Usenet account
>> at Eternal September <http://www.eternal-september.org/> or a similar
>> service and connect to it using a specific tool, such as Thunderbird,
>> Opera, Forte Agent...
>>
>>
>
> Sorry for that. Now I use Thunderbird and aioe. Didn't get eternal
> september running yet. Just get no list of groups and try to figure out
> why.

If I recall correctly there's a FAQ about that in the support group: you
need to enable encryption and make sure Thunderbird provides your user
and password.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Checking equal number of <div> and </div> [message #171328 is a reply to message #171297] Thu, 30 December 2010 12:23 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
jwcarlton wrote:

> Thomas 'PointedEars' Lahn wrote:
>> 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?
>>
>> A recursive RegEx to parse HTML has been posted (by me) and can be easily
>> found in the manual; whatever is not matched by it can be analyzed
>> further.
>>
>> However, you should (also) describe the problem you want to solve, not
>> (only) the problem with your approach for a solution, for the approach
>> might be completely wrong.
>
> It's for a message board, where I've implemented Rich HTML posting
> (contentEditable). It works fine except for when someone copy-and-
> pastes from a site that uses DIV tags for layout. In those cases, if
> the poster doesn't grab the entire page, then it's not uncommon for
> the opening or closing DIV tags to be missing.

In that case you should try the suggested approach:

1. See if there is any <div …> in the remaining text.
2. If there is, match the recursive RegExp, slightly adapted for
DIV elements, against that.
a. If it matches, the tags are balanced in the matched part.
Continue at 1.
3. See if there is any <div …> in the remaining text.
a. If yes, you have more <div …> than </div>. Append a </div> until
the RegExp matches the whole text.
b. If no, you have more </div> than <div …>. Either remove the </div>
at the end, or prepend a <div> at the start, until the RegExp
matches the whole text.

Please trim your quotes to the parts you are referring to,
which especially means: Usually DO NOT quote signatures. TIA.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Re: Checking equal number of <div> and </div> [message #171336 is a reply to message #171304] Thu, 30 December 2010 13:19 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/30/2010 3:37 AM, jwcarlton wrote:
> On Dec 29, 11:59 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 12/29/2010 8:19 PM, 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);
>>> }
>>
>> Get an editor that is html aware. It makes your life much simpler.
>>
>
> No, I mean code submitted by the site visitor, not my own code.

OK, that wasn't clear from your first post.

You're not going to be able to correct all of the errors easily, but the
suggestion of using DOM to help is a good one. It will help a lot. But
you don't want to correct the errors - how do you know the correction
itself is correct? You want to just give the user an error message.

However, I'd take a different approach - I'd put in a preview button
(possibly required) which brings up the prospective page in a new window
to let the user verify it looks ok.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
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 next message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
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`
Re: Checking equal number of <div> and </div> [message #171354 is a reply to message #171283] Thu, 30 December 2010 18:40 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(jwcarlton)

> 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?

You could run the string through Tidy.

Micha
Re: Checking equal number of <div> and </div> [message #171356 is a reply to message #171354] Thu, 30 December 2010 18:55 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 12/30/2010 1:40 PM, Michael Fesser wrote:
> ..oO(jwcarlton)
>
>> 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?
>
> You could run the string through Tidy.
>
> Micha

As an alternative try Notepad++. It does a nice job of tying markup tags
together, instantly highlighting the bookends (e.g., <d>...</d> or
<ul>...</ul>). There are a number of similar markup editors out there. I
tried this one first, liked it, and did not explore any others. Very
good for PHP also.

It does not have a tidy or beautify function, that is left to the markup
artist her or himself.

Bill B
Re: Checking equal number of <div> and </div> [message #171363 is a reply to message #171356] Thu, 30 December 2010 20:52 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 30/12/10 18:55, Bill Braun wrote:
> On 12/30/2010 1:40 PM, Michael Fesser wrote:
>> ..oO(jwcarlton)
>>
>>> 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?
>>
>> You could run the string through Tidy.

> As an alternative try Notepad++. It does a nice job of tying markup tags
> together, instantly highlighting the bookends (e.g., <d>...</d> or
> <ul>...</ul>). There are a number of similar markup editors out there. I
> tried this one first, liked it, and did not explore any others. Very
> good for PHP also.

I think you're both missing the point here.

As I understand it, the issue is html that a website user is entering
probably in a textarea, that the op then incorporates into a webpage.

Like forums and bbcode.

He's not talking about hand-coded pages that he creates himself.

I think the best solution was to read it into a DOM object and then take
the content of the body element wrapped in a new pair of divs as his
content.

Building on another posters suggestions re using DOM.

<?php
// domtest.php
// html fragment cleanup of missing </div> with DOM

function cleanupHTML($source) {
$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTML($source);
libxml_use_internal_errors(FALSE);
$bodies = $dom->GetElementsByTagName("body");
$body = $bodies->item(0);
$newdiv = $dom->saveXML($body);
$newdiv = preg_replace("/^<body>/","<div>",$newdiv);
$newdiv = preg_replace("/<\/body>$/","</div>",$newdiv);
return $newdiv;
}

$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.';

echo "\n>>>>>>>>>>>>>>>\n" . cleanupHTML($fragment) . "\n<<<<<<<<<<<<<<<\n";

?>

and:

$ php5 domtest.php

>>>> >>>>>>>>>>>
<div><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></div>
<<<<<<<<<<<<<<<
$

Rgds

Denis McMahon
Re: Checking equal number of <div> and </div> [message #171364 is a reply to message #171307] Thu, 30 December 2010 20:54 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
Á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
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 next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
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.
Re: Checking equal number of <div> and </div> [message #171367 is a reply to message #171363] Thu, 30 December 2010 21:53 Go to previous message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Denis McMahon)

> On 30/12/10 18:55, Bill Braun wrote:
>> On 12/30/2010 1:40 PM, Michael Fesser wrote:
>>> ..oO(jwcarlton)
>>>
>>>> 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?
>>>
>>> You could run the string through Tidy.
>
>> As an alternative try Notepad++. It does a nice job of tying markup tags
>> together, instantly highlighting the bookends (e.g., <d>...</d> or
>> <ul>...</ul>). There are a number of similar markup editors out there. I
>> tried this one first, liked it, and did not explore any others. Very
>> good for PHP also.
>
> I think you're both missing the point here.

Tidy can be used as an PHP extension.

Micha
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Sat Nov 23 00:50:09 GMT 2024

Total time taken to generate the page: 0.02799 seconds