Php converts html format to text format,

Source: Internet
Author: User
Tags html to text

Php converts html format to text format,

This document describes how to convert html to text in php. We will share this with you for your reference. The details are as follows:

Sometimes you need to convert the html format string to text, but you need to maintain a certain format. For example, you can use the following function to convert a paragraph into a segment format.

function html2text($str){ $str = preg_replace("/<style .*?<\\/style>/is", "", $str); $str = preg_replace("/<script .*?<\\/script>/is", "", $str); $str = preg_replace("/<br \\s*\\/>/i", ">>>>", $str); $str = preg_replace("/<\\/?p>/i", ">>>>", $str); $str = preg_replace("/<\\/?td>/i", "", $str); $str = preg_replace("/<\\/?div>/i", ">>>>", $str); $str = preg_replace("/<\\/?blockquote>/i", "", $str); $str = preg_replace("/<\\/?li>/i", ">>>>", $str); $str = preg_replace("/ /i", " ", $str); $str = preg_replace("/ /i", " ", $str); $str = preg_replace("/&/i", "&", $str); $str = preg_replace("/&/i", "&", $str); $str = preg_replace("/</i", "<", $str); $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("/&#.*?;/i", "", $str); return $str;}

PS: I recommend a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
 
Php code online formatting and beautification tools:Http://tools.jb51.net/code/phpformat

If you are interested in code and code conversion, refer to the online tools on this site:

Online encoding conversion tool (UTF-8/utf-32/Punycode/Base64 ):

Http://tools.jb51.net/transcoding/decode_encode_tool

Online UBB/HTML code conversion tool:

Http://tools.jb51.net/transcoding/ubb2html

Conversion Tool for simplified Chinese characters (Simplified Chinese characters:

Http://tools.jb51.net/transcoding/convertzh

Online image conversion BASE64 tool:

Http://tools.jb51.net/transcoding/img2base64

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.