第一个字母是字符串中的数字

时间:2022-12-06 19:31:17

I was trying to find a quick and easy method to check if the first letter in a string is a number. A lot of the functions and methods I've seen on S.O seem over complicated. I'm wondering, would something like this work:

我试图找到一种快速简便的方法来检查字符串中的第一个字母是否为数字。我在S.O上看到的很多功能和方法看起来都很复杂。我想知道,这样的工作会是这样的:

is_numeric($string[0]);

5 个解决方案

#1


18  

An easier way might be:

一种更简单的方法可能是:

is_numeric(substr($string, 0, 1))

It tackles the problem of a possible empty string (that has no first character) by using substr(). substr() returns false in the case of an empty string, and false is not recognized as a number by is_numeric().

它通过使用substr()解决了可能的空字符串(没有第一个字符)的问题。 substr()在空字符串的情况下返回false,并且is_numeric()不将false识别为数字。

#2


6  

I don't know why that answer is deleted, but the correct answer is

我不知道为什么这个答案被删除了,但正确的答案是

 preg_match('/^\d/', $string);

Why? Because it provides a standard way to query strings. Normally, you have to answer many similar questions in your application:

为什么?因为它提供了查询字符串的标准方法。通常,您必须在应用程序中回答许多类似的问题:

  • does a string start with a digit?
  • 字符串以数字开头?

  • does it contain only digits?
  • 它只包含数字吗?

  • does it end with a letter?
  • 它以字母结尾吗?

  • does it contain a specific substring?
  • 它包含一个特定的子字符串?

etc, etc. Without regular expressions you'd have to invent a different combination of string functions for each case, while REs provide the uniform and standard interface, which you simply reuse over and over again. This is like algebra compared to arithmetic.

如果没有正则表达式,你必须为每种情况发明一个不同的字符串函数组合,而RE提供统一和标准的接口,你可以一遍又一遍地重复使用。这就像代数算术相比。

#3


5  

No, that would not work. You might get "Notice: Uninitialized string offset: 0" notice. To make it work, add strlen():

不,那不行。您可能会收到“通知:未初始化的字符串偏移:0”通知。要使其工作,请添加strlen():

if ( strlen($string) > 0 && is_numeric($string[0]) ) {
}

#4


3  

Yes, it's a clean way of doing it, but use ctype_digit instead since it only allows the numbers 0 to 9 and nothing else.

是的,这是一种干净的方式,但使用ctype_digit,因为它只允许数字0到9而不是其他。

#5


1  

It won't work on empty strings, so you should check the offset prior accessing it:

它不适用于空字符串,因此您应该在访问它之前检查偏移量:

$result = isset($string[0]) ? is_numeric($string[0]) : false;

#1


18  

An easier way might be:

一种更简单的方法可能是:

is_numeric(substr($string, 0, 1))

It tackles the problem of a possible empty string (that has no first character) by using substr(). substr() returns false in the case of an empty string, and false is not recognized as a number by is_numeric().

它通过使用substr()解决了可能的空字符串(没有第一个字符)的问题。 substr()在空字符串的情况下返回false,并且is_numeric()不将false识别为数字。

#2


6  

I don't know why that answer is deleted, but the correct answer is

我不知道为什么这个答案被删除了,但正确的答案是

 preg_match('/^\d/', $string);

Why? Because it provides a standard way to query strings. Normally, you have to answer many similar questions in your application:

为什么?因为它提供了查询字符串的标准方法。通常,您必须在应用程序中回答许多类似的问题:

  • does a string start with a digit?
  • 字符串以数字开头?

  • does it contain only digits?
  • 它只包含数字吗?

  • does it end with a letter?
  • 它以字母结尾吗?

  • does it contain a specific substring?
  • 它包含一个特定的子字符串?

etc, etc. Without regular expressions you'd have to invent a different combination of string functions for each case, while REs provide the uniform and standard interface, which you simply reuse over and over again. This is like algebra compared to arithmetic.

如果没有正则表达式,你必须为每种情况发明一个不同的字符串函数组合,而RE提供统一和标准的接口,你可以一遍又一遍地重复使用。这就像代数算术相比。

#3


5  

No, that would not work. You might get "Notice: Uninitialized string offset: 0" notice. To make it work, add strlen():

不,那不行。您可能会收到“通知:未初始化的字符串偏移:0”通知。要使其工作,请添加strlen():

if ( strlen($string) > 0 && is_numeric($string[0]) ) {
}

#4


3  

Yes, it's a clean way of doing it, but use ctype_digit instead since it only allows the numbers 0 to 9 and nothing else.

是的,这是一种干净的方式,但使用ctype_digit,因为它只允许数字0到9而不是其他。

#5


1  

It won't work on empty strings, so you should check the offset prior accessing it:

它不适用于空字符串,因此您应该在访问它之前检查偏移量:

$result = isset($string[0]) ? is_numeric($string[0]) : false;