本文目錄一覽:
- 1、php中使用正則表達式或其他方法去除html標籤的樣式屬性(不能寫死的)
- 2、php截取字元串以及去掉html標記
- 3、PHP如何可靠的去除HTML標籤。。
- 4、php如何清除html格式並去除文字中的空格然後截取文字
- 5、php文件輸出如何過濾掉html,代碼如下
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如何可靠的去除HTML標籤。。
經測試…strip_tags就可以去掉啊
況且他把script language=’JavaScript’
都去掉了
即使留著document.write(“img src=’abc.gif’/”);也無法起到作用的啊
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,代碼如下
basasasas/b這個html標籤是加粗標籤,如果你想在瀏覽器上顯示的是加粗的asasasas就直接輸出
?php
echo “basasasas/b”;
?
如果你想輸出的basasasas/b這個字元串的話呢
?php
echo htmlspecialchars(“basasasas/b”);
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/232102.html