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 简单数组排序实现代码
Aug 05 PHP
使用PHPMyAdmin修复论坛数据库的图文方法
Jan 09 PHP
php防止网站被刷新的方法汇总
Dec 01 PHP
ThinkPHP提示错误Fatal error: Allowed memory size的解决方法
Feb 12 PHP
windows下安装php的memcache模块的方法
Apr 07 PHP
php页面,mysql数据库转utf-8乱码,utf-8编码问题总结
Aug 27 PHP
PHP中Session和Cookie是如何操作的
Oct 10 PHP
yii2缓存Caching基本用法示例
Jul 18 PHP
PHP Mysqli 常用代码集合
Nov 12 PHP
PHP数据库编程之MySQL优化策略概述
Aug 16 PHP
LNMP部署laravel以及xhprof安装使用教程
Sep 14 PHP
支持汉转拼和拼音分词的PHP中文工具类ChineseUtil
Feb 23 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
header()函数使用说明
2006/11/23 PHP
PHP全概率运算函数(优化版) Webgame开发必备
2011/07/04 PHP
数据库中排序的对比及使用条件详解
2012/02/23 PHP
php调用Google translate_tts api实现代码
2013/08/07 PHP
php根据用户语言跳转相应网页
2015/11/04 PHP
php文件包含目录配置open_basedir的使用与性能详解
2017/04/03 PHP
左侧是表头的JS表格控件(自写,网上没有的)
2013/06/04 Javascript
JQuery伸缩导航练习示例
2013/11/13 Javascript
JavaScript实现简单的数字倒计时
2015/05/15 Javascript
详解Document.Cookie
2015/12/25 Javascript
jQuery实现指定区域外单击关闭指定层的方法【经典】
2016/06/22 Javascript
ES6记录异步函数的执行时间详解
2016/08/31 Javascript
微信小程序 网络请求(GET请求)详解
2016/11/16 Javascript
js中Number数字数值运算后值不对的解决方法
2017/02/28 Javascript
前端主流框架vue学习笔记第二篇
2017/07/26 Javascript
React Navigation 使用中遇到的问题小结
2018/05/08 Javascript
Js利用正则表达式去除字符串的中括号
2020/11/23 Javascript
Python 将Matrix、Dict保存到文件的方法
2018/10/30 Python
浅谈python下含中文字符串正则表达式的编码问题
2018/12/07 Python
python读取图片任意范围区域
2019/01/23 Python
python导入坐标点的具体操作
2019/05/10 Python
详解Django-channels 实现WebSocket实例
2019/08/22 Python
用Python解数独的方法示例
2019/10/24 Python
python针对Oracle常见查询操作实例分析
2020/04/30 Python
python中的django是做什么的
2020/07/31 Python
基于Python正确读取资源文件
2020/09/14 Python
西班牙第一的网上药房:PromoFarma.com
2017/04/17 全球购物
Onzie官网:美国时尚瑜伽品牌
2019/08/21 全球购物
医院义诊活动总结
2014/07/04 职场文书
安全生产目标管理责任书
2014/07/25 职场文书
党员干部观看《周恩来四个昼夜》思想汇报
2014/09/10 职场文书
工程项目合作意向书
2015/05/08 职场文书
2015年度合同管理工作总结
2015/05/22 职场文书
优秀范文:《但愿人长久》教学反思3篇
2019/10/24 职场文书
python调试工具Birdseye的使用教程
2021/05/25 Python
javascript Number 与 Math对象的介绍
2021/11/17 Javascript