本文目录一览:
- 1、PHP正则表达式替换函数eregi_replace不起作用
- 2、PHP怎么样去掉从word直接粘贴过来的没有用的
- 3、正学习PHP,PHP mb_ereg_replace 替换中文的程序,替换不了,谁能帮忙解决下,
PHP正则表达式替换函数eregi_replace不起作用
在5.3版的以后的php,比如5.4, 5.5,已经废除了eregi_replace函数
去掉@,用preg_replace替代。
PHP怎么样去掉从word直接粘贴过来的没有用的
一般处理的方式有二种:
通过编辑器的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 -phpmberegireplace
//'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 -phpmberegireplace
$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'); -phpmberegireplace
$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 -phpmberegireplace
//that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains -phpmberegireplace
//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]xmlw:WordDocumentw:BrowserLevelMicrosoftInternetExplorer4/w:BrowserLevelw:DisplayHorizontalDrawingGridEvery0/w:DisplayHorizontalDrawingGridEveryw:DisplayVerticalDrawingGridEvery2/w:DisplayVerticalDrawingGridEveryw:DocumentKindDocumentNotSpecified/w:DocumentKindw:DrawingGridVerticalSpacing7.8/w:DrawingGridVerticalSpacingw:ViewNormal/w:Vieww:Compatibility/w:Compatibilityw:Zoom0/w:Zoom/w:WordDocument/xml![endif]-- -phpmberegireplace
p 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'; -phpmberegireplace
echo ClearHtml($content,'p');
/*
得到的结果:
p 《优伴户外旅行》--让旅行成为习惯!/p越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!/p
*/
?
正学习PHP,PHP mb_ereg_replace 替换中文的程序,替换不了,谁能帮忙解决下,
将$rg[1]=mb_ereg_replace("谷歌","百度",$rg[1]); 这句替换为$rg[1]=mb_ereg_replace("百度","谷歌",iconv('gb2312', 'utf-8',$rg[1]));-phpmberegireplace