第一个字母是字符串中的数字 [英] First letter is number in string

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

问题描述

我试图找到一种快速简便的方法来检查字符串中的第一个字母是否为数字.我在S.O上看到的许多功能和方法似乎过于复杂.我想知道,这样的事情会起作用吗?

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:

is_numeric($string[0]);

推荐答案

更简单的方法可能是:

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

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

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().

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

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