How to Check, If a PHP String Contains Only English Letters and Digits

How to check, if a php string contains only english letters and digits?

Use preg_match().

if (!preg_match('/[^A-Za-z0-9]/', $string)) // '/[^a-z\d]/i' should also work.
{
// string contains only english letters & digits
}

How do I check if a string is composed only of letters and numbers? (PHP)

You can use the ctype_alnum() function in PHP.

From the manual..

Check for alphanumeric character(s)

Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise.

var_dump(ctype_alnum("æøsads")); // false
var_dump(ctype_alnum("123asd")); // true
  • Live demo at https://3v4l.org/5etr7

Check if string contains only English alphabets , digits and symbols

Would determining if a string is just printable ASCII work? If so you can use this regex:

[ -~]

http://www.catonmat.net/blog/my-favorite-regex/

If you need non ASCII characters as well than you can use the Wikipedia page to get the specific unicode formats that you need:

https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes

How can I check whether is a string contains only English letters?

To match a whole string that only consists of 1 or more lowercase ASCII letters, hyphen or underscores, use

/^[-a-z_]+$/D

See the regex demo

Details:

  • ^ - start of string
  • [-a-z_]+ - 1 or more ASCII lowercase letters, hyphens or underscores
  • $ - end of string
  • /D - the modifier that will make $ match the very end of the string (otherwise, $ will also match a newline that appears at the end of the string).

PHP:

if (preg_match('/^[-a-z_]+$/D', $input)) {
// Yes, all characters of the string are English lowercase letters or dash or underline
} else {
// No, there is at least one unexpected character
}

Allow only English letters and numbers in php

This is how you check whether a string contains only letters from the English alphabet.

if (!preg_match('/[^A-Za-z0-9]/', $string))  {
//string contains only letters from the English alphabet
}

The other question:

strlen(À)

will not return 2. Maybe you meant

strlen('À')

strlen returns

The length of the string on success, and 0 if the string is empty.

taken from here. So, that character is interpreted as two characters, probably due to your encoding.

Check if php string only contains characters from an european language

You can test for Latin characters with \p{Latin} making sure to use the u regex flag:

<?php
$tests = [
'éèäöüßäöüßäöüßäöü',
'abcdeABCDE',
'€, !"§$%&/()=#|<>',
'ÄäAa',
'*',
'Здравствуйте'
];

foreach ($tests as $test) {
if (!preg_match('/[^\p{Latin}0-9€, !"§$%&\/()=#|<>]/u', $test)) {
echo "$test is okay\n";
}
}

Prints:

éèäöüßäöüßäöüßäöü is okay
abcdeABCDE is okay
€, !"§$%&/()=#|<> is okay
ÄäAa is okay

How can I check whether is a string contains only English letters?

To match a whole string that only consists of 1 or more lowercase ASCII letters, hyphen or underscores, use

/^[-a-z_]+$/D

See the regex demo

Details:

  • ^ - start of string
  • [-a-z_]+ - 1 or more ASCII lowercase letters, hyphens or underscores
  • $ - end of string
  • /D - the modifier that will make $ match the very end of the string (otherwise, $ will also match a newline that appears at the end of the string).

PHP:

if (preg_match('/^[-a-z_]+$/D', $input)) {
// Yes, all characters of the string are English lowercase letters or dash or underline
} else {
// No, there is at least one unexpected character
}

How can I check string contains only a-z

Assuming "pure English" = English letters + space:

^[a-zA-Z ]*$
  • [a-zA-Z ] - defines a character set containing lower and upper case letters + space
  • * - repeat any number of times
  • ^$ - make sure the string is matched from the start (^) til the end ($)

How do I check if a string contains a specific word?

Now with PHP 8 you can do this using str_contains:

if (str_contains('How are you', 'are')) { 
echo 'true';
}

RFC

Before PHP 8

You can use the strpos() function which is used to find the occurrence of one string inside another one:

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
echo 'true';
}

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').



Related Topics



Leave a reply



Submit