PHP去掉從word直接粘貼過來(lái)的沒有用格式的函數(shù)
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;
}
測(cè)試使用結(jié)果:
<?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: "宋體";">《優(yōu)伴戶外旅行》——讓旅行成為習(xí)慣!</span></p>越發(fā)忙碌的你,是否想給自己放個(gè)假?專注工作的你,是否還記得上一次鍛煉是什么時(shí)候??jī)?yōu)伴戶外旅行,給你不一樣的旅行體驗(yàn):給心自由,便處處都是風(fēng)景!</span></p>';
echo ClearHtml($content,'<p>');
/*
得到的結(jié)果:
<p >《優(yōu)伴戶外旅行》--讓旅行成為習(xí)慣!</p>越發(fā)忙碌的你,是否想給自己放個(gè)假?專注工作的你,是否還記得上一次鍛煉是什么時(shí)候??jī)?yōu)伴戶外旅行,給你不一樣的旅行體驗(yàn):給心自由,便處處都是風(fēng)景!</p>
*/
?>
- php實(shí)現(xiàn)將上傳word文件轉(zhuǎn)為html的方法
- php導(dǎo)出生成word的方法
- php通過baihui網(wǎng)API實(shí)現(xiàn)讀取word文檔并展示
- PHP字符串word末字符實(shí)現(xiàn)大小寫互換的方法
- PHP把網(wǎng)頁(yè)保存為word文件的三種方法
- php導(dǎo)出word文檔與excel電子表格的簡(jiǎn)單示例代碼
- php導(dǎo)出word格式數(shù)據(jù)的代碼實(shí)例
- 使用PHP導(dǎo)出Word文檔的原理和實(shí)例
- php 備份數(shù)據(jù)庫(kù)代碼(生成word,excel,json,xml,sql)
- php在程序中將網(wǎng)頁(yè)生成word文檔并提供下載的代碼
- PHP中將網(wǎng)頁(yè)導(dǎo)出為Word文檔的代碼
- php實(shí)現(xiàn)word轉(zhuǎn)html的方法
相關(guān)文章
在PHP中靈活使用foreach+list處理多維數(shù)組的方法
這篇文章主要介紹了在PHP中靈活使用foreach+list處理多維數(shù)組的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01分享下頁(yè)面關(guān)鍵字抓取www.icbase.com站點(diǎn)代碼(帶asp.net參數(shù)的)
這篇文章主要介紹了分享下頁(yè)面關(guān)鍵字抓取www.icbase.com站點(diǎn)代碼(帶asp.net參數(shù)的)的相關(guān)資料,需要的朋友可以參考下2014-01-01php實(shí)現(xiàn)數(shù)組篩選奇數(shù)和偶數(shù)示例
這篇文章主要介紹了php實(shí)現(xiàn)數(shù)組篩選奇數(shù)和偶數(shù)示例,需要的朋友可以參考下2014-04-04SQL注入寬字節(jié)注入由淺到深學(xué)習(xí)
這篇文章主要為大家介紹了SQL注入寬字節(jié)注入由淺到深學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02PHP中對(duì)各種加密算法、Hash算法的速度測(cè)試對(duì)比代碼
這篇文章主要介紹了PHP中對(duì)各種加密算法、Hash算法的速度測(cè)試對(duì)比代碼,對(duì)選擇一個(gè)加密算法的同學(xué)可以作為一個(gè)性能考慮因素,需要的朋友可以參考下2014-07-07ThinkPHP5.0多個(gè)文件上傳后找不到臨時(shí)文件的修改方法
這篇文章主要介紹了ThinkPHP5.0多個(gè)文件上傳后找不到臨時(shí)文件的修改方法,需要的朋友可以參考下2018-07-07thinkphp3.2嵌入百度編輯器ueditor的實(shí)例代碼
本篇文章主要介紹了thinkphp3.2嵌入百度編輯器ueditor的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07ajax php傳遞和接收變量實(shí)現(xiàn)思路及代碼
在實(shí)際應(yīng)用中會(huì)遇到這種情況:ajax與php之間傳遞和接收變量,為了實(shí)現(xiàn)這一功能應(yīng)用,很多新手朋友們,只能望洋興嘆啊,于是本人搜集整理一些實(shí)用技巧,曬出來(lái)和大家分享,希望可以幫助你們2012-12-12PHP中使用gettext解決國(guó)際化問題的例子(i18n)
這篇文章主要介紹了PHP中使用gettext解決國(guó)際化問題的例子,即實(shí)現(xiàn)i18n,需要的朋友可以參考下2014-06-06