Code for converting phphtml 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 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:

  1. Example
  2. IsYanshare! ";
  3. Echo strip_tags ($ input ,"");
  4. ?>

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:

  1. Example@example.com ";
  2. Echo strip_tags ($ input );
  3. ?>

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:

  1. Function html2text ($ str ){
  2. $ Str = preg_replace ("/ < style> /Is "," ", $ str); $ str = preg_replace ("/ <>
  3. $ Str = preg_replace ("/ /I "," n ", $ str );
  4. $ Str = preg_replace ("/ /I "," nn ", $ str );
  5. $ Str = preg_replace ("/ /I "," n ", $ str );
  6. $ Str = preg_replace ("/ /I "," n ", $ str );
  7. $ Str = preg_replace ("/ /I "," n ", $ str );
  8. $ Str = preg_replace ("/ /I "," n ", $ str );
  9. $ Str = preg_replace ("// I", "", $ str );
  10. $ Str = preg_replace ("// I", "", $ str );
  11. $ Str = preg_replace ("/&/I", "&", $ str );
  12. $ Str = preg_replace ("/&/I", "&", $ str );
  13. $ Str = preg_replace ("/
  14. $ Str = preg_replace ("/</I", "<", $ str );
  15. $ Str = preg_replace ("/"/I ", '"', $ str );
  16. $ Str = preg_replace ("/& ldquo/I", '"', $ str );
  17. $ Str = preg_replace ("/'/I", "'", $ str );
  18. $ Str = preg_replace ("/& lsquo/I", "'", $ str );
  19. $ Str = preg_replace ("/'/I", "'", $ str );
  20. $ Str = preg_replace ("/& rsquo/I", "'", $ str );
  21. $ Str = preg_replace ("/>/I", ">", $ str );
  22. $ Str = preg_replace ("/>/I", ">", $ str );
  23. $ Str = preg_replace ("/"/I ", '"', $ str );
  24. $ Str = preg_replace ("/& rdquo/I", '"', $ str );
  25. $ Str = strip_tags ($ str );
  26. $ Str = html_entity_decode ($ str, ENT_QUOTES, "UTF-8 ");
  27. $ Str = preg_replace ("/& amp ("/&#.*?; /I "," ", $ str );
  28. Return $ str;
  29. }

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.