note 34067 added to ref.strings

From: Date: Tue, 15 Jul 2003 13:51:28 +0000
Subject: note 34067 added to ref.strings
Groups: php.notes 
Request: Send a blank email to php-notes+get-52133@lists.php.net to get a copy of this message
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]]]); ---- Manual Page -- http://www.php.net/manual/en/ref.strings.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34067 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34067&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34067&report=yes

« previous php.notes (#52133) next »