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 #171363 is a reply to message #171356] Thu, 30 December 2010 20:52 Go to previous messageGo to previous message
Denis McMahon is currently offline  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
[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: Sat Nov 23 10:28:42 GMT 2024

Total time taken to generate the page: 0.07975 seconds