`preg_match` PHP 中的整个字符串 [英] `preg_match` the Whole String in PHP

查看:57
本文介绍了`preg_match` PHP 中的整个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这件事真的让我很困惑,请原谅我的无知.

This thing really confuses me, pardon my ignorance.

我在这里有一个 var $name ,我想摆脱数字,我不希望其中包含任何数字.我的 preg_match() 是这样的:

I have a var $name in here that I want to be free from number, I don't want any digit to be included in it. My preg_match() is this:

var_dump(preg_match('/[^\d]/',$name));

测试用例:

$name = "213131"; // int(0) 
$name = "asdda"; // int(1)
$name = "as232dda"; // int(1)

我想要的是第三种情况也是 int(0).

What I want is to have the third case to be int(0) too.

我真的很难理解这个 preg_match(),文档说如果模式匹配主题,它返回 1.在我的例子中,我使用了一个否定类.

I'm really a hard time understanding this preg_match(), docs say it return 1 if a pattern match a subject. Here in my case, I use a negated class.

推荐答案

#3 匹配,因为您有字母和数字.你的英文正则表达式基本上是

#3 matches because you have both letters and numbers. Your regex in English basically says

如果有非数字字符则匹配

it matches if there is a non-digit character

如果你想匹配非数字字符,你必须让正则表达式匹配整个字符串并允许任意数量的字符:

If you want to match only non-digit characters, you have to have the regex match against the entire string and allow for an arbitrary number of characters:

^[^\d]+$

这篇关于`preg_match` PHP 中的整个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆