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

How to check for alphabets in php?

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)

$strings = array('KjgWZC''arf12');
foreach (
$strings as $testcase) {
    if (
ctype_alpha($testcase)) {
        echo 
"The string $testcase consists of all letters.\n";
    } else {
        echo 
"The string $testcase does not consist of all letters.\n";
    }
}
?>

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:

setLocale

(LC_CTYPE, 'FR_fr.UTF-8');?>

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)

$strings = array('AbCd1zyZ9''foo!#$bar');
foreach (
$strings as $testcase) {
    if (
ctype_alnum($testcase)) {
        echo 
"The string $testcase consists of all letters or digits.\n";
    } else {
        echo 
"The string $testcase does not consist of all letters or digits.\n";
    }
}
?>

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:

$sUser = 'my_username01';
$aValid = array('-', '_');

if(!

ctype_alnum(str_replace($aValid, '', $sUser))) {
    echo
'Your username is not properly formatted.';
}
?>

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.

(ctype_alnum(-1));
// UNIX: bool(false)
// Windows: bool(true)

Anonymous

9 years ago

Quicktip: If ctype is not enabled by default on your server, replace ctype_alnum($var) with preg_match('/^[a-zA-Z0-9]+$/', $var).

Rory

12 years ago

Just for the record, Gentoo doesn't include this function by default. You'll have to recompile PHP with the "ctype" USE flag.

How check string is English or not in PHP?

There is a very simple way of representing this in PHP. Here is a PHP function which will return true if the string contains english characters or it will return false if there are foreign characters. So, if you were to pass the function: is_english('hello');

Is alphanumeric in PHP?

PHP | ctype_alnum() (Check for Alphanumeric) A ctype_alnum() function in PHP used to check all characters of given string/text are alphanumeric or not. If all characters are alphanumeric then return TRUE, otherwise return FALSE.

How do you check if a string contains a number in PHP?

The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.

How do I get the first character of a string in PHP?

To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.