How do I check if a String contains the same letter twice consecutively in php?

And*_*ann 12 html php regex

I don't see why my code doesn't work, any suggestions?

$pattern_c_sap='/\.\-/';
$local='.................';
$local_array = explode( '', $local );

for($i=0; $i<=$local_length; $i++){
if(preg_match($pattern_c_sap , $local_array[$i]) && preg_match($pattern_c_sap , $local_array[$i+1])) {
    return false;
}
}
Run Code Online (Sandbox Code Playgroud)

I had the following errors: The regex should be

[/\.\-/]
Run Code Online (Sandbox Code Playgroud)

and I should have used str_split instead of explode when splitting for each letter.

Jer*_*rry 18

您可以使用preg_match以下内容:

if(preg_match('/(.)\1/', $local, $match){
    echo "contains consecutive characters";
}
Run Code Online (Sandbox Code Playgroud)

(.) 捕获一个角色.

\1 指捕获的角色.

所以,如果你有AA,在(.)捕捉A\1将意味着A.所以正则表达式将检查AA.


gop*_*h.m 5

if (preg_match('/(.)\1/', $str))
    echo "Has 2 same characters consecutively!";
Run Code Online (Sandbox Code Playgroud)


gee*_*dev 5

警告:explode():第5行的此脚本中的空分隔符

$local_array = explode('', $local);
Run Code Online (Sandbox Code Playgroud)

修复将是爆炸构造的正确分隔符