strip_tags($str)
去掉 HTML 及 PHP 的標記
語法: string strip_tags(string str);
傳回值: 字串
函式種類: 資料處理
內容說明 :
解析:本函式可去掉字串中包含的任何 HTML 及 PHP 的標記字串。若是字串的 HTML 及 PHP 標簽原來就有錯,例如少了大於的符號,則也會傳回錯誤。而本函式和
fgetss() 有着相同的功能
PHP去除html、css樣式、js格式的方法很多,但發現,它們基本都有一個弊端:空格往往清除不了
經過不斷的研究,最終找到了一個理想的去除html包括空格css樣式、js 的PHP函數。
PHP清除html、css、js格式並去除空格的PHP函數
function cutstr_html($string, $sublen)
{
$string = strip_tags($string);
$string = preg_replace ('/\n/is', '', $string);
$string = preg_replace ('/ | /is', '', $string);
{
$string = strip_tags($string);
$string = preg_replace ('/\n/is', '', $string);
$string = preg_replace ('/ | /is', '', $string);
$string = preg_replace ('/ /is', '', $string);
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, $t_string);
if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…";
else $string = join('', array_slice($t_string[0], 0, $sublen));
return $string;
}
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, $t_string);
if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…";
else $string = join('', array_slice($t_string[0], 0, $sublen));
return $string;
}
解析:這個函數既有去除html標簽、css樣式、js、空格等格式的功能(格式化html文本)也有截取字符串的功能。
htmlspecialchars 將特殊字元轉成 HTML 格式
語法: string htmlspecialchars(string string);
傳回值: 字串
函式種類: 資料處理
內容說明
解析:本函式將特殊字元轉成 HTML 的字串格式 ( &....; )。最常用到的場合可能就是處理客戶留言的留言版了。
& (和) 轉成 &
" (雙引號) 轉成 "
< (小於) 轉成 <
> (大於) 轉成 >
此函式只轉換上面的特殊字元,並不會全部轉換成 HTML 所定的 ASCII 轉換。
使用范例
<FORM ACTION='./../b'la>
<H2>Restaurant Description<H2>
Name of restaurant:
<INPUT TYPE=text NAME="restname" VALUE="<?echo HTMLSpecialChars($restname); ?>">
<!-- 變數 $restname 是醬子的 $restname="\"The White Horse\""; -->
<BR>
輸入描述 (若您會 HTML,可直接使用): <BR>
<TEXTAREA NAME="descript"><?echo HTMLSpecialChars($descript);?></TEXTAREA>
<INPUT TYPE=submit>
</FORM>
htmlentities
將所有的字元都轉成 HTML 字串
語法: string htmlentities(string string);
傳回值: 字串
函式種類: 資料處理
內容說明
解析:本函式有點像 htmlspecialchars() 函式,但本函式會將所有 string 的字元都轉成 HTML 的特殊字集字串。不過在轉換后閱讀網頁原始碼的方面,會有很多困擾,尤其是網頁原始碼的中文字會變得不知所雲,瀏覽器上看到的還是正常的。
php 去除html標簽 js 和 css樣式 - 最愛用的一個PHP清楚html格式函數
Function ClearHtml($content) {
$content = preg_replace("/<a[^>]*>/i", "", $content);
$content = preg_replace("/<\/a>/i", "", $content);
$content = preg_replace("/<div[^>]*>/i", "", $content);
$content = preg_replace("/<\/div>/i", "", $content);
$content = preg_replace("/<!--[^>]*-->/i", "", $content);//注釋內容
$content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除樣式
$content = preg_replace("/face=.+?['|\"]/",'',$content);//去除樣式只允許小寫正則匹配沒有帶 i 參數
return
$content;
}
