Home > Backend Development > PHP Tutorial > php如何判断字符串中有连续4个的字符

php如何判断字符串中有连续4个的字符

WBOY
Release: 2016-06-13 13:34:00
Original
1019 people have browsed it

php怎么判断字符串中有连续4个的字符

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php $str = 'php怎么判断字符串中有连续4个的字符我我我我我';
//$str = 'php怎么判断字符串中有连续4个的字符我我我我我sssssssss';
if(preg_match("/(.)\\1{3,}/",$str)){
    echo '有连续4个的字符';
} else {
    echo '没有连续4个的字符';
}
?>
Copy after login


以上代码,只可判断英文,汉字不可用啊,有其它方法吗?

------解决方案--------------------
UTF-8 汉字

if(preg_match("/([\x{4E00}-\x{9FFF}])\\1{3,}/u",$str)){
------解决方案--------------------
对于 utf-8
preg_match("/(.)\\1{3,}/u",$str)

对于 gbk
preg_match("/([\x80-\xff].|.)\\1{3,}/",$str)

一个字符串不可能同时具有两种编码
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template