Hello I want to search within a text string, consecutive characters such as:
qwerty, ertyu, tyuiop
asdfgh, dfghjk, fghjkl
zxcvbn, xcvbnm
minuscuslas o capital letters indistinctly.
Thank you.
Hello I want to search within a text string, consecutive characters such as:
qwerty, ertyu, tyuiop
asdfgh, dfghjk, fghjkl
zxcvbn, xcvbnm
minuscuslas o capital letters indistinctly.
Thank you.
I made this code that I hope can help you. I added a variable, tolerance, which is the one that will indicate the quantity followed by letters you will detect as a repetition. It works even inverted, but if you do not want it that way, just remove the abs () of the first function.
<?php
function verificar_consecutivos($cadena, $tolerancia) {
$keyb_map = '1234567890|qwertyuiop|asdfghjklñ|zxcvbnm,.-';
$longitud_cadena = strlen($cadena);
if($longitud_cadena < $tolerancia) {
// Si long de la cadena es menor que la cant de caracteres repetidos a buscar
return FALSE;
}
for($i=0; $i<=($longitud_cadena-$tolerancia); $i++) {
$consecutivos = TRUE;
for($j=0; $j<($tolerancia-1); $j++) {
$pos_carac_actual_keyb = strpos($keyb_map, $cadena[$i+$j]);
$pos_carac_siguiente_keyb = strpos($keyb_map, $cadena[$i+$j+1]);
$diferencia_consecutivos = abs($pos_carac_siguiente_keyb - $pos_carac_actual_keyb);
if($diferencia_consecutivos !== 1) {
// Si no son caracteres consecuntivos se especifica y se termina el for.
$consecutivos = FALSE;
$j = $tolerancia; // Terminar el for $j anticipadamente
}
}
if($consecutivos === TRUE) {
// Apenas encuentra un consecutivo, ya no vale la pena seguir buscando mas.
$i = $longitud_cadena; // Termina el for $i
}
}
return $consecutivos;
}
function caracteres_consecutivos($cadena, $tolerancia) {
if(verificar_consecutivos(strtolower($cadena), $tolerancia)) {
echo 'Hay caracteres consecutivos de teclado'.PHP_EOL;
} else {
echo 'No hay caracteres consecutivos'.PHP_EOL;
}
}
caracteres_consecutivos('yasdfgkytrb', 5); // Consecutivos 'asdfg'
caracteres_consecutivos('yaddfggytrb', 4); // No hay 4 consecutivos
caracteres_consecutivos('qwe', 3); // Consecutivos 'qwe'
caracteres_consecutivos('qwe', 6); // No hay 6 consecutivos
caracteres_consecutivos('', 5); // No hay 5 consecutivos
caracteres_consecutivos('lksdrtyu', 4); // Consecutivos 'rtyu'
caracteres_consecutivos('lksduytr', 4); // Consecutivos 'rtyu' (a la inversa)
And the output is:
Hay caracteres consecutivos de teclado
No hay caracteres consecutivos
Hay caracteres consecutivos de teclado
No hay caracteres consecutivos
No hay caracteres consecutivos
Hay caracteres consecutivos de teclado
Hay caracteres consecutivos de teclado
Maybe this code can help you:
<?php
$text1 = "qwerty";
$text2 = "asdfgh";
$text3 = "zxcvbn";
$diccionario_qwerty = [0=>'q', 1=>'w', 2=>'e', 3=>'r', 4=>'t', 5=>'y', 6=>'u', 7=>'i', 8=>'o', 9=>'p',
10=>'a', 11=>'s', 12=>'d', 13=>'f', 14=>'g', 15=>'h', 16=>'j', 17=>'k',
18=>'z', 19=>'x', 20=>'c', 21=>'v', 22=>'b', 23=>'n', 24=>'m', 25=>'q', 26=>''];
$_text1 = str_split($text1);
foreach ($_text1 as &$c)
$c = $diccionario_qwerty[array_search($c, $diccionario_qwerty)+1];
$text1 = implode($_text1);
print_r($text1);
?>
It is not in a function since it would not behave exactly.
A good beginning could be like this:
<?php
$cadena = "biologia"; //aqui hay 2 caracteres consecutivos 'io'
$linea1 = 'qwertyuiop';
$linea2 = 'asdfghjkl';
$linea3 = 'zxcvbnm';
$lineas = [$linea1,$linea2,$linea3];
$longitud = mb_strlen($cadena);
for ($i=0; $i<$longitud; $i++) {
for($j=2; $j<=$longitud-$i; $j++){
$extrae = substr($cadena,$i,$j);
foreach ($lineas as $clave => $valor) {
if(preg_match('/'.$extrae.'/i',$valor) == 1){
print_r('En la cadena '.$cadena.' hay un bloque de caracteres consecutivos en teclado que son: '.$extrae);
echo "<br>";
} //end if
} //end foreach
} //end for with $j
} //end for with $i
The output for this particular string would be:
En la cadena biologia hay un bloque de caracteres consecutivos en teclado que son: io