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!
*/
?>