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正确配置mysql(apache环境)
Aug 28 PHP
用Simple Excel导出xls实现方法
Dec 06 PHP
浅析PHP 按位与或 (^ 、&amp;)
Jun 21 PHP
PHP采集类snoopy详细介绍(snoopy使用教程)
Jun 19 PHP
php显示指定目录下子目录的方法
Mar 20 PHP
phpStudy访问速度慢和启动失败的解决办法
Nov 19 PHP
php操纵mysqli数据库的实现方法
Sep 18 PHP
PHP类型约束用法示例
Sep 28 PHP
基于PHP制作验证码
Oct 12 PHP
PHP简单实现二维数组的矩阵转置操作示例
Nov 24 PHP
php删除一个路径下的所有文件夹和文件的方法
Feb 07 PHP
PHP调用QQ互联接口实现QQ登录网站功能示例
Oct 24 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
推荐Discuz!5的PHP代码高亮显示与实现可运行代码
2007/03/15 PHP
php实现的支持imagemagick及gd库两种处理的缩略图生成类
2014/09/23 PHP
PHP制作万年历
2015/01/07 PHP
php输出全球各个时区列表的方法
2015/03/31 PHP
php使用GD实现颜色渐变实例
2015/06/02 PHP
PHP可变函数学习小结
2015/11/29 PHP
PHP Header失效的原因分析及解决方法
2016/11/16 PHP
php微信公众平台交互与接口详解
2016/11/28 PHP
Yii2 加载css、js 载静态资源的方法
2017/03/10 PHP
laravel5环境隐藏index.php后缀(apache)的方法
2019/10/12 PHP
iframe调用父页面函数示例详解
2014/07/17 Javascript
Angular 常用指令实例总结整理
2016/12/13 Javascript
Vue.JS项目中5个经典Vuex插件
2017/11/28 Javascript
vue项目中跳转到外部链接的实例讲解
2018/09/20 Javascript
vue之延时刷新实例
2019/11/14 Javascript
[00:47]TI7不朽珍藏III——沙王不朽展示
2017/07/15 DOTA
使用Python的PEAK来适配协议的教程
2015/04/14 Python
KMP算法精解及其Python版的代码示例
2016/06/01 Python
Python使用win32com实现的模拟浏览器功能示例
2017/07/13 Python
Python搭建代理IP池实现检测IP的方法
2019/10/27 Python
Python实现图片裁剪的两种方式(Pillow和OpenCV)
2019/10/30 Python
Python3.9新特性详解
2020/10/10 Python
CSS3实现闪烁动画效果的方法
2015/02/09 HTML / CSS
html5指南-3.如何实现html元素拖拽功能
2013/01/07 HTML / CSS
阿玛尼意大利官网:Armani意大利
2018/10/30 全球购物
Perfume’s Club英国官网:购买香水和护肤品
2019/11/02 全球购物
工作个人的自我评价
2014/01/14 职场文书
精彩的广告词
2014/03/19 职场文书
医药营销个人求职信
2014/04/12 职场文书
教师党员个人整改措施
2014/10/27 职场文书
2014年乡镇卫生院工作总结
2014/11/24 职场文书
2019大学毕业晚会主持词
2019/06/21 职场文书
Python Pandas模块实现数据的统计分析的方法
2021/06/24 Python
Django路由层如何获取正确的url
2021/07/15 Python
Appium中scroll和drag_and_drop根据元素位置滑动
2022/02/15 Python
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技