当前位置: 网学 > 编程文档 > PHP > 正文

PHP去掉从word直接粘贴过来的没有用格式的函数

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/06/18

网学网为需要PHP的朋友们搜集整理了PHP去掉从word直接粘贴过来的没有用格式的函数相关资料,希望对各位网友有所帮助!

一般处理的方式有二种: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:Compatibili

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号