本文目錄一覽:
- 1、php使用正則表達式去掉html中的注釋方法
- 2、php如何清除html格式並去除文字中的空格然後截取文字
- 3、php中使用正則表達式或其他方法去除html標籤的樣式屬性(不能寫死的)
- 4、php截取字符串以及去掉html標記
- 5、PHP 如何獲取當前URL並去掉.html
- 6、php 過濾掉html標籤及標籤內的所有內容
php使用正則表達式去掉html中的注釋方法
最近在項目中在需要輸出瀏覽器中的源文件需要去掉html中的注釋,在網上看了很多的方案,不過很多的答案都是一樣的,並不能解決我的問題,於是就自己寫正則表達式,也對正則有了更加深刻的理解。
首先比較基礎的是:
$a
=
‘!–ceshi–ceshi’;
$a
=
preg_replace(‘#!–.*–#’
,
”
,
$a);
var_dump($a);
上面的代碼會輸出ceshi。
但是如果是下面的字符串的話,就不能達到我們希望的效果了
$a
=
‘!–ceshi–ceshi!–ceshi–‘;
$a
=
preg_replace(‘#!–.*–#’
,
”
,
$a);
var_dump($a);
於是我們就把匹配規則改成如下的格式
preg_replace(‘#!–.*?–#’
,
”
,
$a);
但是在html中如果有!–[if
lt
IE
9]ceshi![endif]–這樣的代碼的話是不能去掉的,所以我們需要改進匹配規則,改成以下的格式
preg_replace(‘#!–[^\!\[]*?–#’
,
”
,
$a);
又接着如果html中有script!–ceshi//–/script的代碼,我們又需要改一下我們的匹配規則了,改成了以下格式
preg_replace(‘#!–[^\!\[]*?(?!\/\/)–#’
,
”
,
$a);
這樣的話我基本上就去掉了我需要去掉的html的注釋了!
以上就是小編為大家帶來的php使用正則表達式去掉html中的注釋方法全部內容了,希望大家多多支持腳本之家~
php如何清除html格式並去除文字中的空格然後截取文字
PHP清除html、css、js格式並去除空格的PHP函數
01 function cutstr_html($string,$length=0,$ellipsis=’…’){
02 $string=strip_tags($string);
03 $string=preg_replace(‘/\n/is’,”,$string);
04 $string=preg_replace(‘/ | /is’,”,$string);
05 $string=preg_replace(‘/ /is’,”,$string);
06 preg_match_all(“/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/”,$string,$string);
07 if(is_array($string)!empty($string[0])){
08 if(is_numeric($length)$length){
09 $string=join(”,array_slice($string[0],0,$length)).$ellipsis;
10 }else{
11 $string=implode(”,$string[0]);
12 }
13 }else{
14 $string=”;
15 }
16 return $string;
17 }
php 去除html標籤 js 和 css樣式
01 function clearHtml($content){
02 $content=preg_replace(“/a[^]*/i”,””,$content);
03 $content=preg_replace(“/\/a/i”,””,$content);
04 $content=preg_replace(“/div[^]*/i”,””,$content);
05 $content=preg_replace(“/\/div/i”,””,$content);
06 $content=preg_replace(“/!–[^]*–/i”,””,$content);//注釋內容
07 $content=preg_replace(“/style=.+?[‘|\”]/i”,”,$content);//去除樣式
08 $content=preg_replace(“/class=.+?[‘|\”]/i”,”,$content);//去除樣式
09 $content=preg_replace(“/id=.+?[‘|\”]/i”,”,$content);//去除樣式
10 $content=preg_replace(“/lang=.+?[‘|\”]/i”,”,$content);//去除樣式
11 $content=preg_replace(“/width=.+?[‘|\”]/i”,”,$content);//去除樣式
12 $content=preg_replace(“/height=.+?[‘|\”]/i”,”,$content);//去除樣式
13 $content=preg_replace(“/border=.+?[‘|\”]/i”,”,$content);//去除樣式
14 $content=preg_replace(“/face=.+?[‘|\”]/i”,”,$content);//去除樣式
15 $content=preg_replace(“/face=.+?[‘|\”]/”,”,$content);//去除樣式 只允許小寫 正則匹配沒有帶 i 參數
16 return $content;
17 }
php中使用正則表達式或其他方法去除html標籤的樣式屬性(不能寫死的)
preg_replace(‘/([a-z]+)[^]*/is’, ‘\\1’, $str);
我使用的就是這個正則。
php截取字符串以及去掉html標記
第一個很簡單,就用你提供這個函數就可以。
不過第二個沒有函數能執行。因為如果是英文字符應該佔用1位,但中文不一樣,utf8的一個漢字佔用3個字符,這樣如果出現中英文一起的情況就會出現亂碼。必須自定義函數解決,下面給你一個簡單的
?php
function chinesesubstr($str,$start,$len) {//$str是指字符串,$start指字符串的起始位置,$len指字符串長度
$strlen=$start+$len; //用$strlen存儲字符串的總長度
for($i=0;$i$strlen;$i++) {
if(ord(substr($str,$i,1))0xa0) { //如果字符串中出現漢字,也就是ASC碼大於0xa0的。作出判斷與英文字符不一樣。
$tmpstr.=substr($str,$i,2);
$i++;
}
else
$tmpstr.=substr($str,$i,1);
}
return $tmpstr;
}
?
PHP 如何獲取當前URL並去掉.html
很簡單啊:
?php
//
第一步:你需要得到你的URL:
$URL=$_SERVER[‘HTTP_HOST’]
.
$_SERVER[‘REQUEST_URI’];
//第二步:把得到的URL後面的“.HTML”去掉:
$geturl=str_replace(‘.html’,”,$URL);
echo
$geturl;
?
但是,可但是:
你的這個頁面應該是php的才對吧,如果是html的,就一定是應用了擬靜態技術來重寫URL,這樣的話,上面的代碼你也可以使用,如果是生成的
靜態頁面
,那麼很不好意思,這個基本上不可行了。也無法達到你的意願。
php 過濾掉html標籤及標籤內的所有內容
方法一:使用strip_tags()函數
strip_tags() 函數剝去字符串中的 HTML、XML 以及PHP的標籤。
使用案例:
$string = “p這裡是潘旭博客/p”
$newStr = strip_tags($string);
echo $newStr;
方法二:使用str_replace()函數
str_replace() 函數以其他字符替換字符串中的一些字符(區分大小寫)
使用案例:
$string = “p這裡是潘旭博客/p”;
$newStr = str_replace(array(“p”,”/p”),array(“”,””));
echo $newStr;
另外還有一種是通過正則的方法,請參考:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/193977.html