note 108976 modified in function.strip-tags by danbrown

From: Date: Fri, 08 Jun 2012 20:15:21 +0000
Subject: note 108976 modified in function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188484@lists.php.net to get a copy of this message
I used substr() to create a summary... for HTML content this is a problem because of unclosed tags and broken tags (if the content is cut off in the middle of an open or close tag)&#13;&#10;&#13;&#10;I came up with this... I am not sure it works 100% of the time, but it seems to do the trick.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;public static function closetags($html)&#13;&#10;&#9;{&#13;&#10;&#9;&#9;// strip fraction of open or close tag from end (e.g. if we take first x characters, we might cut off a tag at the end!)&#13;&#10;&#9;&#9;$html = preg_replace(&#39;/&#60;[^&#62;]*$/&#39;,&#39;&#39;,$html); // ending with fraction of open tag&#13;&#10;&#9;&#9;&#13;&#10;&#9;&#9;// put open tags into an array&#13;&#10;&#9;&#9;preg_match_all(&#39;#&#60;([a-z]+)(?: .*)?(?&#60;![/|/ ])&#62;#iU&#39;, $html, $result);&#13;&#10;&#9;&#9;$opentags = $result[1];&#13;&#10;&#13;&#10;&#9;&#9;// put all closed tags into an array&#13;&#10;&#9;&#9;preg_match_all(&#39;#&#60;/([a-z]+)&#62;#iU&#39;, $html, $result);&#13;&#10;&#9;&#9;$closetags = $result[1];&#13;&#10;&#13;&#10;&#9;&#9;$len_opened = count($opentags);&#13;&#10;&#13;&#10;&#9;&#9;// if all tags are closed, we can return&#13;&#10;&#9;&#9;if (count($closetags) == $len_opened) {&#13;&#10;&#9;&#9;&#9;return $html;&#13;&#10;&#9;&#9;}&#13;&#10;&#13;&#10;&#9;&#9;// close tags in reverse order that they were opened&#13;&#10;&#9;&#9;$opentags = array_reverse($opentags);&#13;&#10;&#13;&#10;&#9;&#9;// self closing tags&#13;&#10;&#9;&#9;$sc = array(&#39;br&#39;,&#39;input&#39;,&#39;img&#39;,&#39;hr&#39;,&#39;meta&#39;,&#39;link&#39;);&#13;&#10;&#9;&#9;// ,&#39;frame&#39;,&#39;iframe&#39;,&#39;param&#39;,&#39;area&#39;,&#39;base&#39;,&#39;basefont&#39;,&#39;col&#39; &#13;&#10;&#9;&#9;// should not skip tags that can have content inside!&#13;&#10;&#13;&#10;&#9;&#9;for ($i=0; $i &#60; $len_opened; $i++)&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;$ot = strtolower($opentags[$i]);&#13;&#10;&#9;&#9;&#9;if (!in_array($opentags[$i], $closetags) &#38;&#38; !in_array($ot,$sc))&#13;&#10;&#9;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;&#9;$html .= &#39;&#60;/&#39;.$opentags[$i].&#39;&#62;&#39;;&#13;&#10;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;&#9;else&#13;&#10;&#9;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;&#9;unset($closetags[array_search($opentags[$i], $closetags)]);&#13;&#10;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;return $html;&#13;&#10;&#9;}&#13;&#10;?&#62; --was-- I used substr() to create a summary... for HTML content this is a problem because of unclosed tags and broken tags (if the content is cut off in the middle of an open or close tag) I came up with this... I am not sure it works 100% of the time, but it seems to do the trick. <pre> public static function closetags($html) { // strip fraction of open or close tag from end (e.g. if we take first x characters, we might cut off a tag at the end!) $html = preg_replace('/<[^>]*$/','',$html); // ending with fraction of open tag // put open tags into an array preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result); $opentags = $result[1]; // put all closed tags into an array preg_match_all('#</([a-z]+)>#iU', $html, $result); $closetags = $result[1]; $len_opened = count($opentags); // if all tags are closed, we can return if (count($closetags) == $len_opened) { return $html; } // close tags in reverse order that they were opened $opentags = array_reverse($opentags); // self closing tags $sc = array('br','input','img','hr','meta','link'); // ,'frame','iframe','param','area','base','basefont','col' // should not skip tags that can have content inside! for ($i=0; $i < $len_opened; $i++) { $ot = strtolower($opentags[$i]); if (!in_array($opentags[$i], $closetags) && !in_array($ot,$sc)) { $html .= '</'.$opentags[$i].'>'; } else { unset($closetags[array_search($opentags[$i], $closetags)]); } } return $html; } </pre> http://php.net/manual/en/function.strip-tags.php

« previous php.notes (#188484) next »