找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 719|回复: 0
打印 上一主题 下一主题

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

[复制链接]

2536

主题

2536

帖子

7532

积分

论坛元老

Rank: 8Rank: 8

积分
7532
跳转到指定楼层
楼主
发表于 2018-2-14 08:27:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            一般处理的方式有二种:1.通过编辑器的JS直接去除。2.提交到后台后,直接用程序去掉无效标签。下面我就分享一个通过PHP的处理方式,成功率可能不是100%。这程序也是在PHP官网上看到的,就顺便粘贴过来了。
[U]复制代码[/U] 代码如下:
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
//']*>(.*?)#isu', '#]*>(.*?)#isu', '#]*>(.*?)#isu');
$replace = array('$2', '$2', '$1');
$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("/\/isu', '', $content);
}
return $content;
}

测试使用结果:
[U]复制代码[/U] 代码如下:
MicrosoftInternetExplorer402DocumentNotSpecified7.8Normal0
《优伴户外旅行》——让旅行成为习惯!
越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!
';
echo ClearHtml($content,'');
/*
得到的结果:
《优伴户外旅行》--让旅行成为习惯!
越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!

*/
?>
            
            
您可能感兴趣的文章:
  • PHP中将网页导出为Word文档的代码
  • php在程序中将网页生成word文档并提供下载的代码
  • php 备份数据库代码(生成word,excel,json,xml,sql)
  • 使用PHP导出Word文档的原理和实例
  • php导出word格式数据的代码实例
  • php导出word文档与excel电子表格的简单示例代码
  • PHP把网页保存为word文件的三种方法
  • PHP字符串word末字符实现大小写互换的方法
  • php实现将上传word文件转为html的方法
  • php通过baihui网API实现读取word文档并展示
  • php导出生成word的方法
  • php实现word转html的方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端