设为首页 收藏本站
查看: 732|回复: 0

[经验分享] PHP怎么样去掉从word直接粘贴过来的没有用的格式

[复制链接]

尚未签到

发表于 2015-8-27 09:35:35 | 显示全部楼层 |阅读模式
通常我们会遇到直接把word内的内容,直接粘贴到文本编辑器中。这时候会出现在文本编辑器中有一些word内的没用的标签内容。一般处理的方式有二种:1.通过编辑器的JS直接去除。2.提交到后台后,直接用程序去掉无效标签。下面我就分享一个通过PHP的处理方式,成功率可能不是100%。这程序也是在PHP官网上看到的,就顺便粘贴过来了。
  
  



1 function ClearHtml($content,$allowtags='') {
2            
3             mb_regex_encoding('UTF-8');
4             //replace MS special characters first
5             $search = array('/‘/u', '/’/u', '/“/u', '/”/u', '/—/u');
6             $replace = array('\'', '\'', '"', '"', '-');
7             $content = preg_replace($search, $replace, $content);
8             //make sure _all_ html entities are converted to the plain ascii equivalents - it appears
9             //in some MS headers, some html entities are encoded and some aren't
10             $content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
11             //try to strip out any C style comments first, since these, embedded in html comments, seem to
12             //prevent strip_tags from removing html comments (MS Word introduced combination)
13             if(mb_stripos($content, '/*') !== FALSE){
14                 $content = mb_eregi_replace('#/\*.*?\*/#s', '', $content, 'm');
15             }
16             //introduce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be
17             //'<1' becomes '< 1'(note: somewhat application specific)
18             $content = preg_replace(array('/<([0-9]+)/'), array('< $1'), $content);
19            
20             $content = strip_tags($content, $allowtags);
21             //eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
22             $content = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $content);
23             //strip out inline css and simplify style tags
24             $search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu');
25             $replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>');
26             $content = preg_replace($search, $replace, $content);
27            
28             //on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears
29             //that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
30             //some MS Style Definitions - this last bit gets rid of any leftover comments */
31             $num_matches = preg_match_all("/\<!--/u", $content, $matches);
32             if($num_matches){
33                 $content = preg_replace('/\<!--(.)*--\>/isu', '', $content);
34             }
35             return $content;
36 }
  测试使用结果:



<?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"><spanyes"; font-size: 12.0000pt; font-family: "宋体";">《优伴户外旅行》——让旅行成为习惯!</span></p>越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!</span></p>';
echo ClearHtml($content,'<p>');
/*
得到的结果:
<p  >《优伴户外旅行》--让旅行成为习惯!</p>越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!</p>
*/
?>

  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-104845-1-1.html 上篇帖子: <转>关于php的出错输出 下篇帖子: PHP得到某段时间 区间的时间戳,php 定时任务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表