How to check for alphabets in php?

How to check if the string has alphabets in PHP

The ctype_alpha and ctype_digit doesn't help in it. is their any method?

asked Mar 17, 2014 at 15:17

7

You can use preg_match for this.

if[preg_match["/[a-z]/i", $string]]{
    print "it has alphabet!";
}

If you want to check for all the characters to be alphabet, then use anchor around the regex. For example ^[a-z]+$.

answered Mar 17, 2014 at 15:18

Sabuj HassanSabuj Hassan

37.2k12 gold badges73 silver badges84 bronze badges

Posted as requested

/^[a-zA-Z]+$/ alpha, /^[0-9]+$/ num, /^[a-zA-Z0-9]+$/ alpha-num

or

/^\pL+$/u Uni alpha, /^\pN+$/u Uni num, /^[\pL\pN]+$/u Uni alpha-num

answered Mar 17, 2014 at 17:20

[PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8]

ctype_alphaCheck for alphabetic character[s]

Description

ctype_alpha[mixed $text]: bool

Parameters

text

The tested string.

Note:

If an int between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character [negative values have 256 added in order to allow characters in the Extended ASCII range]. Any other integer is interpreted as a string containing the decimal digits of the integer.

Warning

As of PHP 8.1.0, passing a non-string argument is deprecated. In the future, the argument will be interpreted as a string instead of an ASCII codepoint. Depending on the intended behavior, the argument should either be cast to string or an explicit call to chr[] should be made.

Return Values

Returns true if every character in text is a letter from the current locale, false otherwise. When called with an empty string the result will always be false.

Examples

Example #1 A ctype_alpha[] example [using the default locale]

The above example will output:

The string KjgWZC consists of all letters.
The string arf12 does not consist of all letters.

See Also

  • ctype_upper[] - Check for uppercase character[s]
  • ctype_lower[] - Check for lowercase character[s]
  • setlocale[] - Set locale information

Tyrunur

13 years ago

It works if you set the locale correctly:



with this change you will get "yes" "yes"

[PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8]

ctype_alnumCheck for alphanumeric character[s]

Description

ctype_alnum[mixed $text]: bool

Parameters

text

The tested string.

Note:

If an int between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character [negative values have 256 added in order to allow characters in the Extended ASCII range]. Any other integer is interpreted as a string containing the decimal digits of the integer.

Warning

As of PHP 8.1.0, passing a non-string argument is deprecated. In the future, the argument will be interpreted as a string instead of an ASCII codepoint. Depending on the intended behavior, the argument should either be cast to string or an explicit call to chr[] should be made.

Return Values

Returns true if every character in text is either a letter or a digit, false otherwise. When called with an empty string the result will always be false.

Examples

Example #1 A ctype_alnum[] example [using the default locale]

The above example will output:

The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.

See Also

  • ctype_alpha[] - Check for alphabetic character[s]
  • ctype_digit[] - Check for numeric character[s]
  • setlocale[] - Set locale information

thinice at gmail dot com

13 years ago

ctype_alnum[] is a godsend for quick and easy username/data filtering when used in conjunction with str_replace[].

Let's say your usernames have dash[-] and underscore[_] allowable and alphanumeric digits as well.

Instead of a regex you can trade a bit of performance for simplicity:

marcelocamargo at linuxmail dot org

6 years ago

It is also important to note that the behavior of `ctype_alnum` differs according to the operating system. For UNIX-based operating system, if you pass a value that is not a string [or an overloaded object], independently of the value, it will always result in false. However, if we do the same on Windows, using, for example, -1 as literal [a minus and a number greater than 0], we'll have true as result.

Chủ Đề