對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2011/11/2 16:00:13
[PHP]將html tags過濾掉的小方法
下面這個小function可以把html的tag過濾掉。
[code]
function striptext($document){
$search = array("' 'si", //style 先清掉
"' 'si", // strip out javascript
"'<[/!]*?[^<>]*?>'si", // strip out html tags
"'([rn])[s]+'", // strip out white space
"'&(quot|#34|#034|#x22);'i", // replace html entities
"'&(amp|#38|#038|#x26);'i", // added hexadecimal values
"'&(lt|#60|#060|#x3c);'i",
"'&(gt|#62|#062|#x3e);'i",
"'&(nbsp|#160|#xa0);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&(reg|#174);'i",
"'&(deg|#176);'i",
"'&(#39|#039|#x27);'",
"'&(euro|#8364);'i", // europe
);
$replace = array( "","",
"",
"1",
""",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
chr(174),
chr(176),
chr(39),
chr(128),
);
$text = preg_replace($search,$replace,$document);
return $text;
}
[code]
原文出處:[PHP]將html tags過濾掉的小方法 @ Bun's Java PHP XOOPS Note :: 隨意窩 Xuite日誌
下面這個小function可以把html的tag過濾掉。
[code]
function striptext($document){
$search = array("' 'si", //style 先清掉
"' 'si", // strip out javascript
"'<[/!]*?[^<>]*?>'si", // strip out html tags
"'([rn])[s]+'", // strip out white space
"'&(quot|#34|#034|#x22);'i", // replace html entities
"'&(amp|#38|#038|#x26);'i", // added hexadecimal values
"'&(lt|#60|#060|#x3c);'i",
"'&(gt|#62|#062|#x3e);'i",
"'&(nbsp|#160|#xa0);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&(reg|#174);'i",
"'&(deg|#176);'i",
"'&(#39|#039|#x27);'",
"'&(euro|#8364);'i", // europe
);
$replace = array( "","",
"",
"1",
""",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
chr(174),
chr(176),
chr(39),
chr(128),
);
$text = preg_replace($search,$replace,$document);
return $text;
}
[code]
原文出處:[PHP]將html tags過濾掉的小方法 @ Bun's Java PHP XOOPS Note :: 隨意窩 Xuite日誌
冷日補充:
大家可能會問,PHP裡面不是已經有Function可以做這事情了嗎?
那位啥冷日還要轉這東西呢?
因為冷日覺得這個正規表示式寫法很可愛,可以運用在任何支援正規表示式的地方!
怎麼應用就看大家囉!