过滤非法字符
❶ 如何过滤高亮显示非法字符
<?//我只是做一个测试,如果再用类似tmd的字符串,一定要注意,如果字符串中的字符出现在$StartReplaceHtml或者$EndReplaceHtml中,你需要修改下面的preg_replace中的规则
$CheckedMessage="Hello!!他X的..他....X...的....How are you m d "; //the message to be checked
echo $CheckedMessage."<br>";
$CheckedWords=array("他X的","tmd");//要过滤的非法字符,如果你需要把单个的字符也过滤,比如:他ererX000的过滤出来他,X,的,你就没必要把过滤字符设置成"他X的"只要设置成$CheckedWords=array("他","X","的");就可以了
$StartReplaceHtml="<u><b><font color='#FF0000'>";
$EndReplaceHtml="</font></b></u>";
$CheckOther= true;//设置标示位,是否显示单个的字符,如果设置成true,下面的if(strstr……)就不需要了
for($i=0;$i<count($CheckedWords);$i++){
if(strstr($CheckedMessage,$CheckedWords[$i])){ //此处可去掉
$CheckedMessage=eregi_replace($CheckedWords[$i],$StartReplaceHtml.$CheckedWords[$i].$EndReplaceHtml,$CheckedMessage);//如果只过滤诸如"他X的"字符串(是字符串,不是单个字符),可以直接写这句,同时把$CheckOther设置成false}if($CheckOther == true){
$CharStringLength = strlen($CheckedWords[$i]);
for($j=0;$j<$CharStringLength;$j++){
$AssumeLength=1;//假定截取长度
if(ord(substr($CheckedWords[$i],$j,$AssumeLength))>0xa0){//如果汉字,假定长度加一
$AssumeLength++;}$SubstrChar = substr($CheckedWords[$i],$j,$AssumeLength);
$CheckedMessage=preg_replace("/(?<!fon)".$SubstrChar."/",$StartReplaceHtml.$SubstrChar.$EndReplaceHtml,$CheckedMessage);//替换字符,同时如果字符含有t的时候<font>中的t不会被替换,如果需要过滤掉在$StartReplace或者在$EndReplaceHtml中的的字符,需要修改规则,否则的话将会出现乱码
if($AssumeLength!=1){//如果当前截取字符为汉字$j++;}}}}echo $CheckedMessage;
>
❷ 怎么限制过滤非法字符注册
^咋不用正则?
var checkStrpass = document.registerUser.user.value;
re=/[^\w\-]/g;
if (re.test(checkStrpass))
{
alert("您输入的用户名包含无效回字符答!");
document.registerUser.user.focus();
return (false);
}
❸ java过滤非法字符的filter
filter代码在pujia12345提供的代码上改的;
jsp页面的编码你设成你自己的,我用的是utf-8。
input.jsp输入后,正常跳转到handle.jsp,而禁词已经被过滤。
filter:
package test;
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class MyFilter implements Filter
{
private List<String> unString;
public void init(FilterConfig filterConfig) throws ServletException
{
unString = new ArrayList<String>();
unString.add("日");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
String content = request.getParameter("content");//需要过滤的参数
if(content!=null){
for (int i = 0; i < unString.size(); i++)
{
String strIllegal = unString.get(i);
if (content.indexOf(strIllegal) >= 0)
{
content = content.replaceAll(strIllegal, "");//非法字符替换成空
}
request.setAttribute("content", content);//为request设置属性保存修改后的值
}
}
chain.doFilter(request, response);
}
public void destroy()
{
//System.out.println("过滤器销毁");
}
}
//---------------------------//
web.xml:
<filter>
<filter-name>myfilter</filter-name>
<filter-class>test.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//---------------------------//
输入页面input.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>input.jsp</title>
</head>
<body>
<form action="handle.jsp" method="post">
<input type="text" name="content" />
<input type="submit" value=" 提交 " />
</form>
</body>
</html>
//---------------------------//
input提交的页面handle.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> handle.jsp </title>
</head>
<body>
<%
String content = (String)request.getAttribute("content");
out.println(content);
%>
</body>
</html>
❹ php过滤非法字符
帮你写了个函数,要用时,调用一下就可以了,希望对你有帮组回
function safe_string($str){ //过滤安全字答符
$str=str_replace("'","",$str);
$str=str_replace('"',"",$str);
$str=str_replace(" ","$nbsp;",$str);
$str=str_replace("\n;","<br/>",$str);
$str=str_replace("<","<",$str);
$str=str_replace(">",">",$str);
$str=str_replace("\t"," ",$str);
$str=str_replace("\r","",$str);
$str=str_replace("/[\s\v]+/"," ",$str);
return $str;
}
❺ javascript 怎样过滤非法字符
你可以用过滤器来过过滤,jsp中的filter。
public class WordFilter implements Filter {
//写自己的response
class MyResponse extends HttpServletResponseWrapper{
//放字符串的
private StringWriter sw = new StringWriter();
//1.这个构造是必须是,作用是把原来的传进来进行替换
public MyResponse(HttpServletResponse arg0) {
super(arg0);
}
//2. 重写方法
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(sw);
}
//3.重写toString
@Override
public String toString() {
return sw.toString();
}
}
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
//替换自己的response
MyResponse response = new MyResponse((HttpServletResponse) arg1);
//让自己的response通过
arg2.doFilter(arg0, response);
//得到自己的内容
String str = response.toString();
//改一改内容
str = str.replaceAll("sb", "s*");
str = str.replaceAll("王八蛋", "??");
//传内容
response.getResponse().getOutputStream().print(str);
System.out.println("...");
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
❻ 如何去除字符串中的非法字符
引用 4 楼 qgylovelj 的回复:首先你要知道你的乱码是用什么表示的一般情况是用?表示如果只有?版就用如下列子权 string sss = "其他描?鲂畔?"; sss = sss.Replace("?", ""); Console.WriteLine(sss); 输出:其他描鲂畔 如果还有其他什么奇怪的东西,最好用正则表达式,这样可以一下过滤掉。这样岂不是先要找到所有的乱码再REPLACE了
❼ 求过滤SQL非法字符的函数
Function CheckSql() '防止SQL注入
Dim sql_injdata
SQL_injdata = "'||exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare"
SQL_inj = split(SQL_Injdata,"|")
If Request.QueryString<>"" Then
For Each SQL_Get In Request.QueryString
For SQL_Data=0 To Ubound(SQL_inj)
if instr(Request.QueryString(SQL_Get),Sql_Inj(Sql_DATA))>0 Then
Response.Write "<Script Language='javascript'>{alert('请不要在参数中包含非法字符!');history.back(-1)}</Script>"
Response.end
end if
next
Next
End If
If Request.Form<>"" Then
For Each Sql_Post In Request.Form
For SQL_Data=0 To Ubound(SQL_inj)
if instr(Request.Form(Sql_Post),Sql_Inj(Sql_DATA))>0 Then
Response.Write "<Script Language='javascript'>{alert('请不要在参数中包含非法字符!');history.back(-1)} </Script>"
Response.end
end if
next
next
end if
End Function
❽ 过滤多个输入框的非法字符
||function checkOtherChar(str,errmsg) {
for(var loop_index=0; loop_index<str.length; loop_index++)
{
if(str.charAt(loop_index) == '~'
||.charAt(loop_index) == '!'
||str.charAt(loop_index) == '@'
||str.charAt(loop_index) == '#'
||str.charAt(loop_index) == '$'
||str.charAt(loop_index) == '%'
||str.charAt(loop_index) == '^'
||str.charAt(loop_index) == '&'
||str.charAt(loop_index) == '*'
||str.charAt(loop_index) == '('
||str.charAt(loop_index) == ')'
||str.charAt(loop_index) == '+'
||str.charAt(loop_index) == '{'
||str.charAt(loop_index) == '}'
||str.charAt(loop_index) == '|'
||str.charAt(loop_index) == ':'
||str.charAt(loop_index) == '"'
||str.charAt(loop_index) == '<'
||str.charAt(loop_index) == '>'
||str.charAt(loop_index) == '?'
||str.charAt(loop_index) == '`'
||str.charAt(loop_index) == '='
||str.charAt(loop_index) == '['
||str.charAt(loop_index) == ']'
||str.charAt(loop_index) == '\\'
||str.charAt(loop_index) == ';'
||str.charAt(loop_index) == '\''
||str.charAt(loop_index) == ','
||str.charAt(loop_index) == '.'
||str.charAt(loop_index) == '-'
||str.charAt(loop_index) == '/')
{
//alert("~,,,!,@,#,$,%,^,&,*,+,`,\',\",:,(,),[,],{,},<,>,|,\\ and / are illegal. Please re-input.");
alert(errmsg);
return false;
}
}//end of for(loop_index)
return true;
}
❾ 在asp中怎么过滤用户输入的非法字符
<%
dim texts
texts=request("表单明抄")
dim kill '要过滤的字符
kill="墙,垃,围殴" '比喻
dim rsss
rsss=split(kill,",")
dim i,xxxx,yyyy
for i=0 to ubound(rsss)
xxxx=len(rsss(i))
dim j
for j=1 to xxxx
yyyy=yyyy&"*"
next
dim zzzz '过滤后的字符串
if i=0 then zzzz=texts
'如果你想把过滤字符换成*
zzzz=replace(zzzz,rsss(i),yyyy)
'如果你想把过滤字符直接去掉
zzzz=replace(zzzz,rsss(i),"")
next
'zzzz就是过滤后的字符了
%>
❿ 求教过滤非法字符的问题
|for (; i<sizeof(a)/sizeof(char); i++) {
if (a[i] < '0' || a[i] > '9')
; a[offset++] = a[i++]; }
改为
for (; i<sizeof(a)/sizeof(char); i++) {
if (a[i] < '0' || a[i] > '9')
continue ;
a[offset++] = a[i];
}