There are many ways to convert html tags into plain text in php. for example, if php comes with the strip_tags function, it can directly convert html into plain text format, next I will take a look at the various
There are many ways to convert html tags into plain text in php. for example, if php comes with the strip_tags function, it can directly convert html into plain text format, next, let's take a look at various conversion codes.
Let's take a look at the strip_tags () function usage. the following example deletes all the tags other than the tags. the code is as follows:
- Example
- IsYanshare! ";
- Echo strip_tags ($ input ,"");
- ?>
Input result: This example. the connection and connection content are filtered out. if you want to retain the content in A, refer to the following code.
Strip_tags has an optional parameter allowable_tags that can be skipped during this process. The following example uses strip_tags () to delete the HTML mark in the string. the code is as follows:
- Example@example.com ";
- Echo strip_tags ($ input );
- ?>
This returns the following results: Email example@example.com
A custom character set that converts html to a character set without html tags and returns the converted string. the code is as follows:
- Function html2text ($ str ){
- $ Str = preg_replace ("/ < style> /Is "," ", $ str); $ str = preg_replace ("/ <>
- $ Str = preg_replace ("/ /I "," n ", $ str );
- $ Str = preg_replace ("/ /I "," nn ", $ str );
- $ Str = preg_replace ("/ /I "," n ", $ str );
- $ Str = preg_replace ("/ /I "," n ", $ str );
- $ Str = preg_replace ("/ /I "," n ", $ str );
- $ Str = preg_replace ("/ /I "," n ", $ str );
- $ Str = preg_replace ("// I", "", $ str );
- $ Str = preg_replace ("// I", "", $ str );
- $ Str = preg_replace ("/&/I", "&", $ str );
- $ Str = preg_replace ("/&/I", "&", $ str );
- $ Str = preg_replace ("/
- $ Str = preg_replace ("/</I", "<", $ str );
- $ Str = preg_replace ("/"/I ", '"', $ str );
- $ Str = preg_replace ("/& ldquo/I", '"', $ str );
- $ Str = preg_replace ("/'/I", "'", $ str );
- $ Str = preg_replace ("/& lsquo/I", "'", $ str );
- $ Str = preg_replace ("/'/I", "'", $ str );
- $ Str = preg_replace ("/& rsquo/I", "'", $ str );
- $ Str = preg_replace ("/>/I", ">", $ str );
- $ Str = preg_replace ("/>/I", ">", $ str );
- $ Str = preg_replace ("/"/I ", '"', $ str );
- $ Str = preg_replace ("/& rdquo/I", '"', $ str );
- $ Str = strip_tags ($ str );
- $ Str = html_entity_decode ($ str, ENT_QUOTES, "UTF-8 ");
- $ Str = preg_replace ("/& amp ("/&#.*?; /I "," ", $ str );
- Return $ str;
- }