博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP怎么样去掉从word直接粘贴过来的没有用的格式
阅读量:4616 次
发布时间:2019-06-09

本文共 3226 字,大约阅读时间需要 10 分钟。

通常我们会遇到直接把word内的内容,直接粘贴到文本编辑器中。这时候会出现在文本编辑器中有一些word内的没用的标签内容。一般处理的方式有二种:1.通过编辑器的JS直接去除。2.提交到后台后,直接用程序去掉无效标签。下面我就分享一个通过PHP的处理方式,成功率可能不是100%。这程序也是在PHP官网上看到的,就顺便粘贴过来了。

 

 

1 function ClearHtml($content,$allowtags='') { 2              3             mb_regex_encoding('UTF-8'); 4             //replace MS special characters first 5             $search = array('/‘/u', '/’/u', '/“/u', '/”/u', '/—/u'); 6             $replace = array('\'', '\'', '"', '"', '-'); 7             $content = preg_replace($search, $replace, $content); 8             //make sure _all_ html entities are converted to the plain ascii equivalents - it appears 9             //in some MS headers, some html entities are encoded and some aren't10             $content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');11             //try to strip out any C style comments first, since these, embedded in html comments, seem to12             //prevent strip_tags from removing html comments (MS Word introduced combination)13             if(mb_stripos($content, '/*') !== FALSE){14                 $content = mb_eregi_replace('#/\*.*?\*/#s', '', $content, 'm');15             }16             //introduce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be17             //'<1' becomes '< 1'(note: somewhat application specific)18             $content = preg_replace(array('/<([0-9]+)/'), array('< $1'), $content);19             20             $content = strip_tags($content, $allowtags);21             //eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one22             $content = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $content);23             //strip out inline css and simplify style tags24             $search = array('#<(strong|b)[^>]*>(.*?)
#isu', '#<(em|i)[^>]*>(.*?)
#isu', '#
]*>(.*?)#isu');25 $replace = array('
$2', '
$2', '
$1');26 $content = preg_replace($search, $replace, $content);27 28 //on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears29 //that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains30 //some MS Style Definitions - this last bit gets rid of any leftover comments */31 $num_matches = preg_match_all("/\

测试使用结果:

MicrosoftInternetExplorer4
0
2
DocumentNotSpecified
7.8
Normal
0

《优伴户外旅行》——让旅行成为习惯!

越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!

';echo ClearHtml($content,'

');/*得到的结果:

《优伴户外旅行》--让旅行成为习惯!

越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!

*/?>

  

 

 

posted on
2012-10-27 15:50 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/WEBROOT/archive/2012/10/27/2742557.html

你可能感兴趣的文章
Java的内部类真的那么难以理解?
查看>>
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
poj 1654 && poj 1675
查看>>
运维派 企业面试题1 监控MySQL主从同步是否异常
查看>>
Docker 版本
查看>>
poj 1753 Flip Game
查看>>
在深信服实习是怎样的体验(研发测试岗)
查看>>
Linux免密码登陆
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>