Re: Checking equal number of <div> and </div> [message #171363 is a reply to message #171356] |
Thu, 30 December 2010 20:52 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
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
|
|
|