Remove HTML tag in PHP-PHP Tutorial

Source: Internet
Author: User
Remove HTML tags from PHP. We usually use htmlspecialchars () to filter html, but escape the html characters and display the html source code. use strip_tags () to remove html tags. zookeeper

We usually use htmlspecialchars () to filter html, but escape the html characters and display the html source code.

Use strip_tags () to remove html tags.

$ Str = 'href ';
// Echo htmlspecialchars ($ str );
Echo strip_tags ($ str );

?>

Many websites have a small part of an article on the homepage. here, we need to use strip_tags () to remove the html tag. however, we still need to consider the encoding of Chinese characters, because it is easy to cut the last Chinese character into half by cutting the field string normally.

/**
* Intercepting UTF-8 strings
* @ Since 2008.12.23
* @ Param string $ string intercepted by str
* @ Param integer $ start position
* @ Param integer $ 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 );
}

Escape () is used to filter html, but the html characters are escaped. the html source code is displayed. the html tag can be removed using strip_tags ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.