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 相关文章推荐
fleaphp crud操作之findByField函数的使用方法
Apr 23 PHP
PHP session会话的安全性分析
Sep 08 PHP
使用pthreads实现真正的PHP多线程(需PHP5.3以上版本)
May 05 PHP
PHP实现的oracle分页函数实例
Jan 25 PHP
全面了解PHP中的全局变量
Jun 17 PHP
php字符串比较函数用法小结(strcmp,strcasecmp,strnatcmp及strnatcasecmp)
Jul 18 PHP
PHP实现大数(浮点数)取余的方法
Feb 18 PHP
基于PHP的加载类操作以及其他两种魔术方法的应用实例
Aug 28 PHP
PHP实现字母数字混合验证码功能
Jul 11 PHP
PHP Redis扩展无法加载的问题解决方法
Aug 22 PHP
PHP实现简单用户登录界面
Oct 23 PHP
laravel 创建命令行命令的图文教程
Oct 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
php 广告调用类代码(支持Flash调用)
2011/08/11 PHP
php检测apache mod_rewrite模块是否安装的方法
2015/03/14 PHP
php实现概率性随机抽奖代码
2016/01/02 PHP
PHP输出XML格式数据的方法总结
2017/02/08 PHP
THINKPHP截取中文字符串函数实例代码
2017/03/20 PHP
让IE6支持min-width和max-width的方法
2010/06/25 Javascript
Javascript小技巧之生成html元素
2014/05/15 Javascript
js的window.showModalDialog及window.open用法实例分析
2015/01/29 Javascript
Jquery常用的方法汇总
2015/09/01 Javascript
基于HTML5上使用iScroll实现下拉刷新,上拉加载更多
2016/05/21 Javascript
Vue开发过程中遇到的疑惑知识点总结
2017/01/20 Javascript
Echarts基本用法_动力节点Java学院整理
2017/08/11 Javascript
基于vue-cli、elementUI的Vue超简单入门小例子(推荐)
2019/04/17 Javascript
jQuery操作cookie的示例代码
2019/06/05 jQuery
浅谈Vue组件单元测试究竟测试什么
2020/02/05 Javascript
详细介绍解决vue和jsp结合的方法
2020/02/06 Javascript
JQuery+drag.js上传图片并且实现图片拖曳
2020/11/18 jQuery
如何管理Vue中的缓存页面
2021/02/06 Vue.js
[05:04]DOTA2上海特级锦标赛主赛事第二日TOP10
2016/03/04 DOTA
Python使用xlrd模块操作Excel数据导入的方法
2015/05/26 Python
详解Python计算机视觉 图像扭曲(仿射扭曲)
2019/03/27 Python
python批量识别图片指定区域文字内容
2019/04/30 Python
python语言是免费还是收费的?
2020/06/15 Python
详解用 python-docx 创建浮动图片
2021/01/24 Python
用CSS3实现背景渐变的方法
2015/07/14 HTML / CSS
Stefania Mode美国:奢华设计师和时尚服装
2018/01/07 全球购物
激光脱毛、蓝光和护肤:Tria Beauty
2019/03/28 全球购物
银行演讲稿范文
2014/01/03 职场文书
运动会广播稿50字
2014/01/26 职场文书
大学信息公开实施方案
2014/03/09 职场文书
主办会计岗位职责
2014/03/13 职场文书
节水倡议书
2015/01/19 职场文书
少年雷锋观后感
2015/06/10 职场文书
vue+springboot实现登录验证码
2021/05/27 Vue.js
Java 超详细讲解IO操作字节流与字符流
2022/03/25 Java/Android
SQL Server携程核心系统无感迁移到MySQL实战
2022/06/01 SQL Server