過濾非法字元
❶ 如何過濾高亮顯示非法字元
<?//我只是做一個測試,如果再用類似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];
}