regex - how to search for consecutive repeated characters in a string without using any built-in php functions? -
i have been asked solve question. please suggest me how solve this.
$string = "kkjnmnmnjjjnmn"; here need find consecutively repeated string of length 3. example - kkj has occurred once , jnm 2 times , nmn 3 times.
starting first character, going in right side direction, 3 consecutive characters repeated more once should output.
preg_match_all result nmn - 2 times , not 3 times. how solve this?
$string = "kkjnmnmnjjjnmn"; $length = strlen($string); $pieces = []; ($i = 0; $i < $length - 2; $i++) { $piece = substr($string, $i, 3); if (array_key_exists($piece, $pieces)) { $pieces[$piece] += 1; } else { $pieces[$piece] = 1; } } // $pieces contain need
Comments
Post a Comment