带有 preg_replace 的 PHP 过滤器只允许使用字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7059543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:54:34  来源:igfitidea点击:

PHP filter with preg_replace allow only letters

phpregex

提问by Master345

I have a little problem with preg_replace. I need a function that converts all characters to nothing except [A-z][0-9]and some . ! ?

我对 preg_replace 有点问题。我需要一个函数将所有字符转换为空字符,除了[A-z][0-9]一些. ! ?

I could do a preg_match, but this only verify, and I want to be replaced. This for not putting junk characters like <p>and ;[[;[p;[in description META TAG.

我可以做一个preg_match,但这只是验证,我想被替换。这不把垃圾字符像 <p>;[[;[p;[在描述元标记。

So the script must be like:

所以脚本必须是这样的:

;")<br>kk23?! => brkk23?!

;")<br>kk23?! => brkk23?!

Any help would be appreciated :D

任何帮助将不胜感激:D

回答by dang

$string = ';")<br>kk23?!'; 
$new_string = preg_replace("/[^A-Za-z0-9?!]/",'',$string);
echo $new_string;

leave- letters, numbers, spaces, ?!

离开 - 字母,数字,空格,?!

/* 3 choices. Pick one you like! */
$str = preg_replace("/[^A-Za-z0-9?! ]/","",$str);
$str = preg_replace("/[^A-Za-z0-9?!\s]/","",$str);
$str = preg_replace("/[^A-Za-z0-9?![:space:]]/","",$str);

回答by RiaD

 $var=preg_replace('~[^A-Za-z0-9?.!]~','',$var);

Don't forget A-Za-zand A-zare not same

不要忘记A-Za-zA-z不一样

回答by Aditya P Bhatt

A quick solution will be as below also:

一个快速的解决方案也如下:

if (preg_match('/^[\w\.]+$/', $str)) {
    echo 'Str is valid';
} else
    echo 'Str is invalid';

// string only contain the a to z , A to Z, 0 to 9 and _ (underscore)

// 字符串只包含 a 到 z 、A 到 Z、0 到 9 和 _(下划线)

\w - matches [a-zA-Z0-9_]+

Hope it helps.

希望能帮助到你。

回答by Ben

The easiest way is to just do something similar to: Just add the characters after the !, make sure to escape them if needed.

最简单的方法是做类似的事情:只需在 之后添加字符!,确保在需要时将它们转义。

$string = "<br>kk23?!";
$string = preg_replace('/[^A-Za-z0-9 \?!]/', '', $string);

回答by user3706926

More visit to this page. I think more people getting the same problem. The better way is to try your self and get what you need. Custom yours or copy paste this php and try it :

更多访问此页面。我认为更多的人遇到同样的问题。更好的方法是尝试自己并获得所需的东西。自定义您的或复制粘贴此 php 并尝试:

$sample_input = '&&*9?><<script>}cat-<html>ch(_P.,,mE.:;xc##e*p32t.ion $e){di+-($e->ge69tMesPHP _f0sage()3);}';

$output = ereg_replace("[^..........]", "", $sample_input);        

echo "validate =".$output;

modify by filling this to get what you want :

通过填写此内容进行修改以获得您想要的内容:

 $output = ereg_replace("[^.........]", "", $sample_input);

Example : if you want only lowercase then do like this :

示例:如果你只想要小写,那么这样做:

$output = ereg_replace("[^a-z]", $sample_input);

lower case with white space :

带空格的小写:

 $output = ereg_replace("[^a-z ]", $sample_input);

and more....., This is a simple validation method :

还有更多.....,这是一个简单的验证方法:

$username = ereg_replace("[^A-Z0-9_]", "", $username);
$fullname = ereg_replace("[^A-Za-z0-9., ]", "", $fullname);
$city     = ereg_replace("[^A-Za-z -]", "", $city);
$phone    = ereg_replace("[^0-9 +()-]", "", $phone);
$state    = ereg_replace("[^A-Za-z -]", "", $state);
$zipcode  = ereg_replace("[^0-9]", "", $zipcode);
$country  = ereg_replace("[^A-Za-z -]", "", $country);
$gender   = ereg_replace("[^mf]", "", $gender);

Try yourself, hope will help...

自己试试,希望能帮到你。。。