Home  >  Article  >  Backend Development  >  How to detect if a string has only letters in php

How to detect if a string has only letters in php

青灯夜游
青灯夜游Original
2022-04-22 19:48:364175browse

In php, you can use the preg_match() function with the regular expression "/^[a-zA-Z\s] $/" to detect whether a string has only letters. The syntax "preg_match("/^[a -zA-Z\s] $/",string)"; if the return value is 1, only letters are returned, if 0 is returned, other characters are included.

How to detect if a string has only letters in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php detects whether the string only has letters, in other words That is: Determine whether the string is pure English letters

In PHP, you can use the preg_match() function with a regular expression to check.

Regular expression used: "/^[a-zA-Z\s] $/"

## The #preg_match() function can search and match strings based on regular expressions. The syntax format of the function is as follows:

preg_match($pattern,$subject [, &$matches [, $flags = 0 [, $offset = 0 ]]])

The parameter description is as follows:

    $pattern: the pattern to be searched , which is the edited regular expression;
  • $subject: the string to be searched;
  • $matches: optional parameters (array type), if $matches is provided, it will is populated as search results. $matches[0] contains the text matched by the complete pattern, $matches[1] contains the text matched by the first capture subgroup, and so on;
  • $flags: optional parameter, $flags can Is set to PREG_OFFSET_CAPTURE. If this tag is passed, for each occurrence of a match, the string offset (relative to the target string) will be appended to the return;
  • $offset: optional parameter, Used to specify where to start searching in the target string (unit is bytes).
preg_match() function can return the number of matches for $pattern. Its value will be 0 times (no match) or 1 time, because preg_match() will stop searching after the first match. .

If the return value is 1, it contains only letters, and if the return value is 0, it contains other characters.

Example:


<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
echo preg_match("/^[a-zA-Z\s]+$/","abcddfkj")."<br>";
echo preg_match("/^[a-zA-Z\s]+$/","ab12kj");
?>

How to detect if a string has only letters in php

This kind of output is not very good. Let’s customize a function and do a process to return true if there are only letters. Otherwise return false


function is_english($str)
{
    if(preg_match("/^[a-zA-Z\s]+$/",$str)){
        return true;
    }
    return false;
}

Call the function to determine whether the string is composed of pure English letters

var_dump(is_english(&#39;php.cn&#39;)); 
// bool(false)   含有标点符号所以返回 false  
var_dump(is_english(&#39;phpcn&#39;));  
// bool(true)
var_dump(is_english(&#39;phpcn123&#39;)); 
// bool(false)   含有123
var_dump(is_english(&#39;欢迎来到这里&#39;)); 
// bool(false)   都是中文

How to detect if a string has only letters in php

Recommended learning: "

PHP Video Tutorial

The above is the detailed content of How to detect if a string has only letters in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn