/** * 去掉字符串中的html标签 * 使用方法: * <code> * $input = '<a href="http://test/test.php" title="example">hello</a>'; * $output = Typecho_Common::stripTags($input, <a href="">); * echo $output; * //display: '<a href="http://test/test.php">hello</a>' * </code> * * @access public * @param string $string 需要处理的字符串 * @param string $allowableTags 需要忽略的html标签 * @return string */ public static function stripTags($html, $allowableTags = NULL) { if (!empty($allowableTags) && preg_match_all("/\\<([a-z]+)([^>]*)\\>/is", $allowableTags, $tags)) { self::$_allowableTags = '|' . implode('|', $tags[1]) . '|'; if (in_array('code', $tags[1])) { $html = self::lockHTML($html); } $normalizeTags = '<' . implode('><', $tags[1]) . '>'; $html = strip_tags($html, $normalizeTags); $attributes = array_map('trim', $tags[2]); $allowableAttributes = array(); foreach ($attributes as $key => $val) { $allowableAttributes[$tags[1][$key]] = array_keys(self::__parseAtttrs($val)); } self::$_allowableAttributes = $allowableAttributes; $len = strlen($html); $tag = ''; $attrs = ''; $pos = -1; $quote = ''; $start = 0; for ($i = 0; $i < $len; $i++) { if ('<' == $html[$i] && -1 == $pos) { $start = $i; $pos = 0; } else { if (0 == $pos && '/' == $html[$i] && empty($tag)) { $pos = -1; } else { if (0 == $pos && ctype_alpha($html[$i])) { $tag .= $html[$i]; } else { if (0 == $pos && ctype_space($html[$i])) { $pos = 1; } else { if (1 == $pos && (!empty($quote) || '>' != $html[$i])) { if (empty($quote) && ('"' == $html[$i] || "'" == $html[$i])) { $quote = $html[$i]; } else { if (!empty($quote) && $quote == $html[$i]) { $quote = ''; } } $attrs .= $html[$i]; } else { if (-1 != $pos && empty($quote) && '>' == $html[$i]) { $out = self::__tagFilter($tag, $attrs); $outLen = strlen($out); $nextStart = $start + $outLen; $tag = ''; $attrs = ''; $html = substr_replace($html, $out, $start, $i - $start + 1); $len = strlen($html); $i = $nextStart - 1; $pos = -1; } } } } } } } $html = preg_replace_callback("/<\\/([_0-9a-z-]+)>/is", array('Typecho_Common', '__closeTagFilter'), $html); $html = self::releaseHTML($html); } else { $html = strip_tags($html); } //去掉注释 return preg_replace("/<\\!\\-\\-[^>]*\\-\\->/s", '', $html); }