note 34067 deleted from ref.strings by tomsommer

From: Date: Wed, 15 Sep 2004 20:47:33 +0000
Subject: note 34067 deleted from ref.strings by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-76786@lists.php.net to get a copy of this message
Note Submitter: ---- Not long ago, i built a forum with PHP where HTML was allowed. One Problem than was, that some users used HTML but didn't close all tags, so the design of the page containing the entry was damaged. So I decided to make a function which closes all open Tags. It can also be used for closing tags of your own Code ([b] or so, as used in many boards) 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; } string closeOpenTags(string string [, string beginChar [, stringEndChar [, string CloseChar]]]);

« previous php.notes (#76786) next »