A. php 如何从mysql 一个字段中提取内容并过滤所有HTML代码,然后存入另一个字段内
本人不擅长写正则,正则部分 你自己看着写吧
步骤 1 首先测试整理procts_short_description
1.1 定义一个 变量内容为 procts_description 的某一个完整内容
1.2 用正则过滤掉不想要的东西, 将想要的东西整理好
注:这个做完 你已经完成很大一部分内容了
2 备份数据库表 procts,将procts复制成 procts_test表。为了测试 不破坏procts表
3 查询出procts_test表内容 。获取到每行数据的时候 执行下 步骤1 的内容
4 将步骤一相关正确的结果 放置到一个数组中 产品id为key ,形成的正确结果为value
5 最终 遍历正确的数组。 执行更新 procts_test 表
6 当确认 以上无误 ,那你就可以更改procts 表了
B. php如何处理html标签
需要用到一个函数实现,htmlentities,就是将字符串转换成html实体,用法你可以查一下php手册,同时它还有个反函数,htmlspecialchars_decode
C. php文件输出如何过滤掉html,代码如下
<b>asasasas</b>这个html标签是加粗标签,如果你想在浏览器上显示的是加粗的asasasas就直接输出
<?php
echo "<b>asasasas</b>";
?>
如果你想输出的<b>asasasas</b>这个字符串的话呢
<?php
echo htmlspecialchars("<b>asasasas</b>");
?>
D. 用php过滤html部分标签
$str=preg_replace("/\s+/", " ", $str); //过滤多余回车
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)
$str=preg_replace("/<\!--.*?-->/si","",$str); //注释
$str=preg_replace("/<(\!.*?)>/si","",$str); //过滤DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si","",$str); //过滤html标签
$str=preg_replace("/<(\/?head.*?)>/si","",$str); //过滤head标签
$str=preg_replace("/<(\/?meta.*?)>/si","",$str); //过滤meta标签
$str=preg_replace("/<(\/?body.*?)>/si","",$str); //过滤body标签
$str=preg_replace("/<(\/?link.*?)>/si","",$str); //过滤link标签
$str=preg_replace("/<(\/?form.*?)>/si","",$str); //过滤form标签
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签
$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(\/?applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(\/?style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(\/?title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(\/?objec.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/<(\/?script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签
$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //过滤script标签
$str=preg_replace("//si","&#",$str); //过滤script标签,如javAsCript:alert(
清除空格,换行
function DeleteHtml($str)
{
$str = trim($str);
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str);
return trim($str);
}
过滤HTML属性
1,过滤所有html标签的正则表达式:
复制代码 代码如下:
</?[^>]+>
//过滤所有html标签的属性的正则表达式:
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html);
3,过滤部分html标签的正则表达式的排除式(比如排除<p>,即不过滤<p>):
复制代码 代码如下:
</?[^pP/>]+>
4,过滤部分html标签的正则表达式的枚举式(比如需要过滤<a><p><b>等):
复制代码 代码如下:
</?[aApPbB][^>]*>
5,过滤部分html标签的属性的正则表达式的排除式(比如排除alt属性,即不过滤alt属性):
复制代码 代码如下:
\s(?!alt)[a-zA-Z]+=[^\s]*
6,过滤部分html标签的属性的正则表达式的枚举式(比如alt属性):
复制代码 代码如下:
(\s)alt=[^\s]*
E. php如何过滤编辑器的html标签
选择1.将特殊符号进行转换,可以用htmlspecialchars把<变为“<”等
选择2.用正则表达式替换,将标签都删除:
$content=preg_replace('/\<.+?\>/','',$content);
F. php正则表达式过滤某些HTML标签代码
如果只要
<b>
标签,不用“过滤”的方法,用“提取”的方法更简单。
$str
=
'<img
src="xxx"><b>aaa</b><br>\n<b>b\nbb</b><span
style="color:#FF0000;">yyy</span>';
$pattern
=
'/<b>(((?!<\/b>).)*)<\/b>/mi';
preg_match_all($pattern,
$str,
$matches,
PREG_SET_ORDER);
print_r($matches);
输出
Array
(
[0]
=>
Array
(
[0]
=>
<b>aaa</b>
[1]
=>
aaa
[2]
=>
a
)
[1]
=>
Array
(
[0]
=>
<b>b\nbb</b>
[1]
=>
b\nbb
[2]
=>
b
)
)
$matches[0][0],$matches[1][0]
是你想要的结果?
G. 如何在PHP中成功的过滤危险HTML的代码
//php批量过滤post,get敏感数据
if(get_magic_quotes_gpc()){
$_GET=stripslashes_array($_GET);
$_POST=stripslashes_array($_POST);
}
functionstripslashes_array(&$array){
while(list($key,$var)=each($array)){
if($key!='argc'&&$key!='argv'&&(strtoupper($key)!=$key||''.intval($key)=="$key")){
if(is_string($var)){
$array[$key]=stripslashes($var);
}
if(is_array($var)){
$array[$key]=stripslashes_array($var);
}
}
}
return$array;
}
//--------------------------
//替换HTML尾标签,为过滤服务
//--------------------------
functionlib_replace_end_tag($str)
{
if(empty($str))returnfalse;
$str=htmlspecialchars($str);
$str=str_replace('/',"",$str);
$str=str_replace("\","",$str);
$str=str_replace(">","",$str);
$str=str_replace("<","",$str);
$str=str_replace("<SCRIPT>","",$str);
$str=str_replace("</SCRIPT>","",$str);
$str=str_replace("<script>","",$str);
$str=str_replace("</script>","",$str);
$str=str_replace("select","select",$str);
$str=str_replace("join","join",$str);
$str=str_replace("union","union",$str);
$str=str_replace("where","where",$str);
$str=str_replace("insert","insert",$str);
$str=str_replace("delete","delete",$str);
$str=str_replace("update","update",$str);
$str=str_replace("like","like",$str);
$str=str_replace("drop","drop",$str);
$str=str_replace("create","create",$str);
$str=str_replace("modify","modify",$str);
$str=str_replace("rename","rename",$str);
$str=str_replace("alter","alter",$str);
$str=str_replace("cas","cast",$str);
$str=str_replace("&","&",$str);
$str=str_replace(">",">",$str);
$str=str_replace("<","<",$str);
$str=str_replace("",chr(32),$str);
$str=str_replace("",chr(9),$str);
$str=str_replace("",chr(9),$str);
$str=str_replace("&",chr(34),$str);
$str=str_replace("'",chr(39),$str);
$str=str_replace("<br/>",chr(13),$str);
$str=str_replace("''","'",$str);
$str=str_replace("css","'",$str);
$str=str_replace("CSS","'",$str);
return$str;
}
H. php过滤多余html标签的代码!
$str = ” This line contains\tliberal \r\n use of whitespace.\n\n”;
$str = trim($str);// 首先去掉头尾空格
$str = preg_replace(’/\s(?=\s)/’, ‘’, $str);// 接着去掉两个空格以上的
$str = preg_replace(’/[\n\r\t]/’, ‘ ‘, $str);// 最后将非空格替换为一个空格
使用上面的例子可以去掉所有多余的空格。
首先使用TRim()去头尾空格,
接着用preg_replace()去掉重复的空格。
当中的(?=)表示只匹配后面的空格跟随前面的空格的空格