English 中文(简体)
清除试验
原标题:RegEx for cleanup a string

该登记表是关于从搜索表格中清除用户投入的。

$query = preg_replace("/[^A-Za-z0-9 _.,*&-]/",    , $query);

我也需要把斜体作为有效特性,但如果我补充的话,我会犯错误。 我假定我不得不逃脱,但找不到这样做的方法。

$query = preg_replace("/[^A-Za-z0-9 _.,*&-/]/",    , $query); // doesn t works
$query = preg_replace("/[^A-Za-z0-9 _.,*&-//]/",    , $query); // doesn t works
$query = preg_replace("/[^A-Za-z0-9 _.,*&-/]/",    , $query); // doesn t works

使用PHP

最佳回答

您可以使用<代码>/的其他东西作为您的划界者,这样可以尝试:

$query = preg_replace("%[^A-Za-z0-9 _.,*&-/]%",    , $query);

Kobe还张贴了在这种情况下逃跑的正确方式,但我发现,如果我把划界者转而可能的话,我会更能读一下。

http://www.ohchr.org。

可以在http://www.php.net/manual/en/regexp.reference.delimiters.php找到一些额外的信息(在这里引用它:)

当使用PCRE函数时,需要将模式用定界符括起来。 定界符可以是任何非字母数字,非反斜杠,非空格字符。

问题回答

您应两度越狱,即:您需要创建“钟码”/“,因此,斜坡也应作为/>越出:

$query = preg_replace("/[^A-Za-z0-9 _.,*&\/-]/",    , $query); 

同时,请确保将 - 移动到末尾,或者也要进行转义处理。它在字符集中有两个字符之间的不同。

$query = preg_replace("/[^A-Za-z0-9 _.,*&-/]/",    , $query);

如果您使用单引号像这样写,就可以工作:

$query = preg_replace( /[^A-Za-z0-9 _.,*&/-]/ ,    , $query);

The cause of this is that strings enclosed in " are parsed for , etc. and $vars. Thus; escaping a / makes PHP try to find a special meaning for "/" as in " " and when it fails it removes the backslash.

用<>包围的字符串不会被解析。

为了转义一个字符,只需在其前面加上反斜杠;但不要忘记你正在使用双引号字符串——这可能是让它更难的原因:你可能需要转义反斜杠本身。


Another solution that I generally use is to work with a different regex delimiter, that you don t have in your regex. For instance, using a # :

$query = preg_replace("#[^A-Za-z0-9 _.,*&-/]#",    , $query);

这应该解决问题 :-)





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签