Code for converting php html format to text format

Source: Internet
Author: User

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.

In the following example, all tags except the <a> tag are deleted:

The Code is as follows: Copy code

<! --? Php <br? --> $ Input = "This <a href =" http://www.bKjia. c0m/"> example </a>
Is <strong> yanshare </strong>! ";
Echo strip_tags ($ input, "<a> ");
? & Gt;
</A>

Input result

This <a href = "http://www.bKjia. c0m/"> example </a>
Is yanshare!

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, which specifies the tag that can be skipped during this process. The following example uses strip_tags () to delete the HTML tag from the string:

The Code is as follows: Copy code


<! --? Php <br? --> $ Input = "Email <a href =" example@example.com "> example@example.com </a> ";
Echo strip_tags ($ input );
? & Gt;
The following results are returned:

Email example@example.com


A custom character set that converts html to a character set without html tags.

The Code is as follows: Copy code

Function html2text ($ str ){
$ Str = preg_replace ("/<style .*? </Style>/is "," ", $ str); $ str = preg_replace ("/<script .*? </Script>/is "," ", $ str );
$ Str = preg_replace ("/<br s */? />/I "," n ", $ str );
$ Str = preg_replace ("/</? P>/I "," nn ", $ str );
$ Str = preg_replace ("/</? Td>/I "," n ", $ str );
$ Str = preg_replace ("/</? Div>/I "," n ", $ str );
$ Str = preg_replace ("/</? Blockquote>/I "," n ", $ str );
$ Str = preg_replace ("/</? Li>/I "," n ", $ str );
$ Str = preg_replace ("/& nbsp;/I", "", $ str );
$ Str = preg_replace ("/& nbsp/I", "", $ str );
$ Str = preg_replace ("/& amp;/I", "&", $ str );
$ Str = preg_replace ("/& amp/I", "&", $ str );
$ Str = preg_replace ("/& lt;/I", "<", $ str );
$ Str = preg_replace ("/& lt/I", "<", $ str );
$ Str = preg_replace ("/& ldquo;/I", '"', $ str );
$ Str = preg_replace ("/& ldquo/I", '"', $ str );
$ Str = preg_replace ("/& lsquo;/I", "'", $ str );
$ Str = preg_replace ("/& lsquo/I", "'", $ str );
$ Str = preg_replace ("/& rsquo;/I", "'", $ str );
$ Str = preg_replace ("/& rsquo/I", "'", $ str );
$ Str = preg_replace ("/& gt;/I", ">", $ str );
$ Str = preg_replace ("/& gt/I", ">", $ str );
$ Str = preg_replace ("/& rdquo;/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;
}

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.