Home >Backend Development >PHP Problem >How to detect whether a string contains Chinese in php

How to detect whether a string contains Chinese in php

青灯夜游
青灯夜游Original
2022-09-22 19:10:556348browse

Detection steps: 1. Use preg_replace() with regular expressions to filter strings and remove non-Chinese letters in the strings. The syntax is "preg_replace("/[^\x{4E00}-\x{9FFF }] /u",'', original string)" will return a filter string containing all Chinese letters; 2. Use "===" to determine whether the filter string is an empty string, the syntax "filter string ===''", if it is equal to, the original string does not contain Chinese characters, otherwise, the original string contains Chinese characters.

How to detect whether a string contains Chinese in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

In PHP, you can use preg_replace () function and the "===" operator to detect whether the string contains Chinese characters.

Implementation steps:

Step 1: Use the preg_replace() function with regular expressions to filter strings and remove them Non-Chinese letters in the string

preg_replace() function can perform regular expression search and replacement; you only need to use regular expressions to search for non-Chinese letters in the string and replace them with Just use the empty character ''.

The regular expression is: /[^\x{4E00}-\x{9FFF}] /u

$pattern = "/[^\x{4E00}-\x{9FFF}]+/u";
$newStr=preg_replace($pattern,'', $str);

How to detect whether a string contains Chinese in php

Will return a filter string containing all Chinese letters

Step 2: Use the "===" operator to determine whether the filter string is an empty string

$newStr===''
  • If equal, the original string does not contain Chinese

  • If not equal, the original string contains Chinese

Implementation code:

function f($str){
	echo "原字符串:";
	var_dump($str);
	$pattern = "/[^\x{4E00}-\x{9FFF}]+/u";
	$newStr=preg_replace($pattern,'', $str);
	if($newStr===''){
		echo "字符串中不包含中文
"; }else{ echo "字符串中包含中文
"; } }

Call the above f($str) function

$str1= 'php中文网!-=1548';
f($str1);
$str2 = "123456";
f($str2);

How to detect whether a string contains Chinese in php

Description:

preg_replace() function

preg_replace() function can perform regular expression search and replacement, yes A powerful string replacement processing function. The syntax format of this function is as follows:

preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]])

Parameter description is as follows:

  • $pattern: The pattern to be searched can be a string or String array;
  • $replacement: String or string array used for replacement. If this argument is a string and $pattern is an array, then all patterns are replaced with this string. If $pattern and $replacement are both arrays, each $pattern is replaced with the corresponding element in $replacement. If there are fewer elements in $replacement than in $pattern, the extra $pattern is replaced with the empty string.
  • $subject: The string or string array to be searched and replaced. If $subject is an array, the search and replacement will be performed on each element of $subject, and the return value will also be one array.
  • $limit: Optional parameter, the maximum number of substitutions per pattern on each $subject. Default is -1 (infinite).
  • $count: Optional parameter, if specified, will be filled with the number of completed substitutions.

If $subject is an array, the preg_replace() function will return an array, otherwise it will return a string.

If the function preg_replace() finds a match, it will return the replaced $subject, otherwise it will return the unchanged $subject. Each parameter of the preg_replace() function (except the parameter $limit) can be an array. If the $pattern parameter and the $replacement parameter are both arrays, the function will process the keys in the order they appear in the array. If an error occurs, NULL is returned.

The parameter $replacement can contain back references \\n or $n, the latter is preferred syntactically. Each such reference will be replaced by the text captured by the nth capturing subgroup that was matched. n can be 0-99, with \\0 and $0 representing the complete pattern matching text.

The serial number counting method of capturing subgroups is: the left bracket representing the capturing subgroup is counted from left to right, starting from 1. If you want to use backslashes in $replacement, you must use 4 ("\\\\" because this is first a php string, and after escaping it is two, and then it is considered as a string after passing through the regular expression engine. an original backslash).

When working in replacement mode and the backreference needs to be followed by another number (for example: adding an original number immediately after a matching pattern), you cannot use the syntax \\1. Describes backreferences. For example, \\11 will make preg_replace() unable to understand whether you want a \\1 backreference followed by an original 1, or a \\11 backreference followed by nothing. The solution in this case is to use ${1}1. This creates a separate backreference for $1, a separate backreference for source 1.

当使用被弃用的 e 修饰符时,这个函数会转义一些字符(即:'、"、\ 和 NULL)然后进行后向引用替换。当这些完成后请确保后向引用解析完后没有单引号或双引号引起的语法错误(比如:'strlen(\'$1\')+strlen("$2")')。确保符合 PHP 的字符串语法,并且符合 eval 语法。因为在完成替换后,引擎会将结果字符串作为 php 代码使用 eval 方式进行评估并将返回值作为最终参与替换的字符串。

恒等式(===)运算符

恒等式(===)运算符是进行给定变量或值之间的严格比较;它比较,并查看两个变量(表达式或常量)是否值相等且具有相同的数据类型,即两者都是字符串或两者都是整数等等。

如果两个变量(表达式或常量)包含相同的值和相同的数据类型,则此运算符返回true,否则返回false。

示例:

<?php 
header("content-type:text/html;charset=utf-8");  
// 给变量赋整数值
$x = 999; 
echo &#39;$x=&#39;.$x."<br>"; 
// 给变量赋字符串值
$y = &#39;999&#39;; 
echo &#39;$y=&#39;.$y."<br>"; 
//比较$x 和$y 
if ($x === $y) 
    echo &#39;$x和$y相等&#39;; 
else
    echo &#39;$x和$y不相等&#39;; 
?>

输出:

How to detect whether a string contains Chinese in php

说明:在上面的例子中,$ x和$ y的值相等但数据类型不同,因而返回false,执行else部分。

推荐学习:《PHP视频教程

The above is the detailed content of How to detect whether a string contains Chinese 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