Three solutions for deleting HTMl tags in PHP _ PHP Tutorial

Source: Internet
Author: User
PHP can delete HTMl tags in three ways. Method 1: directly retrieve the tag you want to retrieve and copy the code as follows :? Php retrieves the br tag functionstrip ($ str) {$ strstr_replace (br, $ str); $ strhtmlspecialchars ($ str ); Method 1:
Directly retrieve the tag

The code is as follows:


// Retrieve the br Mark
Function strip ($ str)
{
$ Str = str_replace ("
"," ", $ Str );
// $ Str = htmlspecialchars ($ str );
Return strip_tags ($ str );
}
?>


Method 2.
PHP has a strip_tags function to easily remove HTML tags.
Echo strip_tags ("Hello World"); // Remove HTML, XML, and PHP labels.
Non-standard HTML code can also be correctly removed:
Echo strip_tags ("\"> cftea "); // output cftea
In PHP, you can use the strip_tags function to remove HTML tags. See the following example:

The code is as follows:


$ Str = 'www

Dreamdu

. Com ';
Echo (htmlspecialchars ($ str )."
");
Echo (strip_tags ($ str ));
?>


Method 3.
The strtr () function converts special characters in a string.
Syntax
Strtr (string, from,)
Or
Strtr (string, array)
Parameters Description
String1 Required. Specifies the string to be converted.
From Required (unless an array is used ). Specifies the character to be changed.
To Required (unless an array is used ). Specifies the character to be changed.
Array Required (unless you useFromAndTo). An array where the key is the original character and the value is the target character.

Example 1:

The code is as follows:


Echo strtr ("Hilla Warld", "ia", "eo ");
?>


Example 2:

The code is as follows:


$ Arr = array ("Hello" => "Hi", "world" => "earth ");
Echo strtr ("Hello world", $ arr );
?>

The token directly retrieves the tag code you want to retrieve :? Php // remove the br flag function strip ($ str) {$ str = str_replace ("br", "", $ str ); // $ str = htmlspecialchars ($ str );...

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.