PHP去掉从word直接粘贴过来的没有用格式的函数


Posted in PHP onOctober 29, 2012

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

function ClearHtml($content,$allowtags='') { mb_regex_encoding('UTF-8'); 
//replace MS special characters first 
$search = array('/‘/u', '/’/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 to 
//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 could 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\s+$/', '/\s\s+/u'), array('', '', ' '), $content); 
//strip out inline css and simplify style tags 
$search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu'); 
$replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>'); 
$content = preg_replace($search, $replace, $content); 
//on some of the ?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("/\<!--/u", $content, $matches); 
if($num_matches){ 
$content = preg_replace('/\<!--(.)*--\>/isu', '', $content); 
} 
return $content; 
}

测试使用结果:
<?php 
$content = ' <!--[if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]--> 
<p class="p0" style="text-indent: 24.0000pt; margin-bottom: 0pt; margin-top: 0pt;"><span style="mso-spacerun: "yes"; font-size: 12.0000pt; font-family: "宋体";">《优伴户外旅行》——让旅行成为习惯!</span></p>越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!</span></p>'; 
echo ClearHtml($content,'<p>'); /* 
得到的结果: 
<p >《优伴户外旅行》--让旅行成为习惯!</p>越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!</p> 
*/ 
?>
PHP 相关文章推荐
网络资源
Oct 09 PHP
php之Memcache学习笔记
Jun 17 PHP
php删除字符串末尾子字符,删除开始字符,删除两端字符(实现代码)
Jun 27 PHP
简单的php+mysql聊天室实现方法(附源码)
Jan 05 PHP
PHP扩展框架之Yaf框架的安装与使用
May 18 PHP
PHP面向对象继承用法详解(优化与减少代码重复)
Dec 02 PHP
thinkPHP简单调用函数与类库的方法
Mar 15 PHP
PHP随机获取未被微信屏蔽的域名(微信域名检测)
Mar 19 PHP
PHP多进程编程实例详解
Jul 19 PHP
PHP文字转图片功能原理与实现方法分析
Aug 31 PHP
PHP分页显示的方法分析【附PHP通用分页类】
May 10 PHP
PHP7 foreach() 函数修改
Mar 09 PHP
php daddslashes()和 saddslashes()有哪些区别分析
Oct 26 #PHP
PHP daddslashes 使用方法介绍
Oct 26 #PHP
Zend Studio去除编辑器的语法警告设置方法
Oct 24 #PHP
真正根据utf8编码的规律来进行截取字符串的函数(utf8版sub_str )
Oct 24 #PHP
php中检查文件或目录是否存在的代码小结
Oct 22 #PHP
php模拟js函数unescape的函数代码
Oct 20 #PHP
PHP 万年历实现代码
Oct 18 #PHP
You might like
php通过COM类调用组件的实现代码
2012/01/11 PHP
使用php实现从身份证中提取生日
2016/05/09 PHP
WordPress中设置Post Type自定义文章类型的实例教程
2016/05/10 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
自动生成文章摘要的代码[JavaScript 版本]
2007/03/20 Javascript
&amp;lt;script defer&amp;gt; defer 是什么意思
2009/05/10 Javascript
FF火狐下获取一个元素同类型的相邻元素实现代码
2012/12/15 Javascript
js实现点击注册按钮开始读秒倒计时的小例子
2013/05/11 Javascript
JS正则表达式获取分组内容的方法详解
2013/11/15 Javascript
使用jquery操作session方法分享
2015/01/22 Javascript
基于JS实现简单的样式切换效果代码
2015/09/04 Javascript
JavaScript电子时钟倒计时
2016/01/09 Javascript
详解js数组的完全随机排列算法
2016/12/16 Javascript
js模糊查询实例分享
2016/12/26 Javascript
五步轻松实现zTree的使用
2017/11/01 Javascript
对angular 实时更新模板视图的方法$apply详解
2018/10/09 Javascript
微信小程序遍历Echarts图表实现多个饼图
2019/04/25 Javascript
JS实现给数组对象排序的方法分析
2019/06/24 Javascript
微信js-sdk 录音功能的示例代码
2019/11/01 Javascript
Vue中keep-alive组件的深入理解
2020/08/23 Javascript
[41:17]完美世界DOTA2联赛PWL S3 access vs CPG 第二场 12.13
2020/12/17 DOTA
python strip() 函数和 split() 函数的详解及实例
2017/02/03 Python
Python与Java间Socket通信实例代码
2017/03/06 Python
python机器学习案例教程——K最近邻算法的实现
2017/12/28 Python
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
2020/02/25 Python
浅谈CSS3 box-sizing 属性 有趣的盒模型
2019/04/02 HTML / CSS
白宫黑市官网:White House Black Market
2016/11/17 全球购物
La Redoute英国官网:法国时尚品牌
2017/04/27 全球购物
草莓网官网:StrawberryNET
2019/08/21 全球购物
DBA数据库管理员JAVA程序员架构师必看
2016/02/07 面试题
请解释一下webService? 如何用.net实现webService
2014/06/09 面试题
酒店工作职员求职简历的自我评价
2013/10/23 职场文书
学校党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
幼儿园个人总结
2015/02/28 职场文书
电台广播稿范文
2015/08/19 职场文书
阿里云服务器部署mongodb的详细过程
2021/09/04 MongoDB