To determine whether a string contains only English characters (including letters and numbers), you can use the following PHP function:
function isEnglishString($input) {
// Regular expression to match only English letters and digits
$pattern = '/^[A-Za-z0-9]*$/i';
return preg_match($pattern, $input) === 1;
}
// Example usage:
$testString1 = "测试";
echo "$testString1 = " . (isEnglishString($testString1) ? 'true' : 'false') . "
";
$testString2 = "test";
echo "$testString2 = " . (isEnglishString($testString2) ? 'true' : 'false') . "
";
?>
The function isEnglishString
uses a regular expression to check if the input string contains only English alphabets (both uppercase and lowercase) and digits. If the string matches this pattern, it returns true
; otherwise, it returns false
.
Note that this function does not account for spaces, punctuation, or special characters. If you need to include these in your checks, you may need to modify the regular expression accordingly.