Home > Article > Backend Development > Remove HTML tags in PHP_PHP tutorial
Usually we use htmlspecialchars() to filter html, but after escaping the characters of html, what is finally displayed is the html source code.
Use strip_tags() to remove html tags.
$str = 'href';
//echo htmlspecialchars($str);
echo strip_tags($str);
?>
Many website homepages have a small part of an article. Here we need to use strip_tags() to remove the html tag. But for Chinese characters, we also need to consider what encoding is used, because it is easy to cut the field string normally and the last A Chinese character cut in half.
/**
* Intercept utf-8 string
* @since 2008.12.23
* @param string $str The intercepted string
* @param integer $start starting position
* @param integer $length interception length (each Chinese character is 3 bytes)
*/
function utf8_strcut($str, $start, $length=null) {
preg_match_all('/./us', $str, $match);
$chars = is_null($length)? array_slice($match[0], $start ) : array_slice($match[0], $start, $length);
unset($str);
return implode('', $chars);
}