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 相关文章推荐
PHP 中执行系统外部命令
Oct 09 PHP
php压缩HTML函数轻松实现压缩html/js/Css及注意事项
Jan 27 PHP
php缓冲 output_buffering的使用详解
Jun 13 PHP
一个经典的PHP验证码类分享
Nov 18 PHP
PHP通过内置函数memory_get_usage()获取内存使用情况
Nov 20 PHP
thinkphp模板继承实例简述
Nov 26 PHP
PHP框架Laravel的小技巧两则
Feb 10 PHP
Laravel 5 学习笔记
Mar 06 PHP
基于yaf框架和uploadify插件,做的一个导入excel文件,查看并保存数据的功能
Jan 24 PHP
PHP实现判断数组是一维、二维或几维的方法
Feb 06 PHP
PHPExcel中文帮助手册|PHPExcel使用方法(分享)
Jun 09 PHP
如何使用php生成zip压缩包
Apr 21 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
千呼万唤始出来,DOTA2勇士令状不朽宝藏Ⅱ现已推出
2020/08/25 DOTA
phpstorm配置Xdebug进行调试PHP教程
2014/12/01 PHP
总结PHP删除字符串最后一个字符的三种方法
2016/08/30 PHP
PHP实现腾讯短网址生成api接口实例
2020/12/08 PHP
从javascript语言本身谈项目实战
2006/12/27 Javascript
IE DOM实现存在的部分问题及解决方法
2009/07/25 Javascript
JQuery将文本转化成JSON对象需要注意的问题
2011/05/09 Javascript
JS 毫秒转时间示例代码
2013/09/22 Javascript
如何让你的Lightbox支持滚轮缩放及Base64图片
2014/12/04 Javascript
JavaScript组件焦点与页内锚点间传值的方法
2015/02/02 Javascript
JS中多种方式创建对象详解
2016/03/22 Javascript
javascript中错误使用var造成undefined
2016/03/31 Javascript
Web 开发中Ajax的Session 超时处理方法
2017/01/19 Javascript
JS获取短信验证码倒计时的实现代码
2017/05/22 Javascript
js实现登录与注册界面
2017/11/01 Javascript
Angular移动端页面input无法输入的解决方法
2017/11/14 Javascript
vue-cli创建的项目,配置多页面的实现方法
2018/03/15 Javascript
基于vue2的canvas时钟倒计时组件步骤解析
2018/11/05 Javascript
微信小程序位置授权处理方法
2019/06/13 Javascript
js事件机制----捕获与冒泡机制实例分析
2020/05/22 Javascript
vue基于Echarts的拖拽数据可视化功能实现
2020/12/04 Vue.js
python3实现二叉树的遍历与递归算法解析(小结)
2019/07/03 Python
简单的Python调度器Schedule详解
2019/08/30 Python
Python +Selenium解决图片验证码登录或注册问题(推荐)
2020/02/09 Python
基于python3的socket聊天编程
2020/02/17 Python
Python中常用的高阶函数实例详解
2020/02/21 Python
What's the difference between an interface and abstract class? (接口与抽象类有什么区别)
2012/10/29 面试题
中英双版中文教师求职信
2013/10/27 职场文书
项目施工员岗位职责
2014/03/09 职场文书
《赶海》教学反思
2014/04/20 职场文书
电子商务专业应届生求职信
2014/05/28 职场文书
中学学校门卫岗位职责
2014/08/15 职场文书
运动会演讲稿200字
2014/08/25 职场文书
2014年平安创建工作总结
2014/11/24 职场文书
党员检讨书范文
2014/12/27 职场文书
Mysql效率优化定位较低sql的两种方式
2021/05/26 MySQL