PHP removes the unformatted functions directly pasted from word.

Source: Internet
Author: User
Tags html comment
Usually we will directly paste the content in the word into the text editor. At this time, some useless tag content in the word will appear in the text editor. There are two general processing methods: 1. directly remove it through the JS of the editor. 2. after the application is submitted to the background, remove the invalid tag using the program. Next I will share a PHP processing method with a success rate of not 100%. This program is also seen on the PHP official website and pasted by the way.
The code is as follows:
Function ClearHtml ($ content, $ allowtags = ''){

Mb_regex_encoding ('utf-8 ');
// Replace MS special characters first
$ Search = array ('/U','/U', '/', '/"/U ', '/-/u ');
$ Replace = array ('\'','\'','"','"','-');
$ Content = preg_replace ($ search, $ replace, $ content );
// Make sure _ all _ html entities are converted to the plain ascii equivalents-it appears
// In some MS headers, some html entities are encoded and some aren't
$ Content = html_entity_decode ($ content, ENT_QUOTES, 'utf-8 ');
// Try to strip out any C style comments first, since these, embedded in html comments, seem
// Prevent strip_tags from removing html comments (MS Word introduced combination)
If (mb_stripos ($ content ,'/*')! = FALSE ){
$ Content = mb_eregi_replace ('#/\*.*? \ */# S', '', $ content, 'M ');
}
// Introduce a space into any arithmetic expressions that cocould be caught by strip_tags so that they won't be
// '<1' becomes' <1' (note: somewhat application specific)
$ Content = preg_replace (array ('/<([0-9] +)/'), array ('<$1'), $ content );

$ Content = strip_tags ($ content, $ allowtags );
// Eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
$ Content = preg_replace (array ('/^ \ s +/', '/\ s + $ /', '/\ s +/u'), array ('','', ''), $ content );
// Strip out inline css and simplify style tags
$ Search = array ('# <(strong | B) [^>] *> (.*?) # Isu', '# <(em | I) [^>] *> (.*?) # Isu ','# ] *> (.*?)# Isu ');
$ Replace = array (' $2',' $2',' $1');
$ Content = preg_replace ($ search, $ replace, $ content );

// On some of? Newer MS Word exports, where you get conditionals of the form 'If gte mso 9', etc., it appears
// That whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
// Some MS Style Definitions-this last bit gets rid of any leftover comments */
$ Num_matches = preg_match_all ("/\

Companion outdoor travel-making travel a habit!

If you are busy, do you want to give yourself a vacation? Do you still remember when the last exercise was? When traveling outdoors, you can enjoy a different travel experience: If you are at your freedom, you can see scenery everywhere!

';
Echo ClearHtml ($ content ,'

');

/*
Result:

Companion outdoor travel-making travel a habit!

If you are busy, do you want to give yourself a vacation? Do you still remember when the last exercise was? When traveling outdoors, you can enjoy a different travel experience: If you are at your freedom, you can see scenery everywhere!


*/
?>

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.