...

View Full Version : Starting/Ending Tags



Jeshua
04-07-2004, 02:01 AM
I was wondering how one would go about searching a string of input (such as in a forum) and make sure any tags started are ended before the end of the post. For example if someone starts a <b> tag i want to be sure that there is a corresponding </b> after it somewhere, and if not, inserting it in.

Nightfire
04-07-2004, 03:45 AM
Would be best to use javascript for it. Then atleast the user wouldn't have to click back or whatever to change something.

DesignersToolz
04-07-2004, 04:09 AM
If you insist on using PHP for this then all you have to do is count the occurances of <b> and makes sure its the same ammount as </b>

Jeshua
04-17-2004, 07:32 PM
Well i had a reason I wanted it in php, so when the person posted they wouldn't have to worry about ending tags if they didn't want to, and when it displays the post it makes sure to end all the tags. I found a function that showed my how to do it though.


function closeOpenTags($str, $open = "<", $close = ">", $end = "/", $tokens = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
{
$chars = array();
for ($i = 0; $i < strlen($tokens); $i++)
{
$chars[] = substr($tokens, $i, 1);
}
$openedTags = array();
$closedTags = array();
$tag = false;
$closeTag = false;
$tagName = "";
for ($i = 0; $i < strlen($str); $i++)
{
$char = substr($str, $i, 1);
if ($char == $open)
{ $tag = true; continue; }
if ($char == $end)
{ $closeTag = true; continue; }
if ($tag && in_array($char, $chars))
{ $tagName .= $char; }
else
{ if ($closeTag)
{ if (isset($closedTags[$tagName]))
{ $closedTags[$tagName]++; }
else
{ $closedTags[$tagName] = 1; } }
elseif ($tag)
{ if (isset($openedTags[$tagName]))
{ $openedTags[$tagName]++; }
else
{ $openedTags[$tagName] = 1; } }
$tag = false; $closeTag = false; $tagName = ""; } }

while(list($tag, $count) = each($openedTags))
{ $closedTags[$tag] = isset($closedTags[$tag]) ? $closedTags[$tag] : 0;
$count -= $closedTags[$tag];
if ($count < 1) continue;
$str .= str_repeat($open.$end.$tag.$close, $count); }

return $str;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum