How to find the largest substring between two equal characters in a string with PHP

1 Answer

0 votes
function GetLongestSubstring($str) {
    $start = 0; $end = 0; $max = 0;
    $size = strlen($str);
    
    for ($i = 0; $i < $size - 1; $i++) {
        for ($j = $i + 1; $j < $size; $j++) {
            if ($str[$i] == $str[$j]) {
                $temp = abs($j - $i - 1);
                if ($temp > $max) {
                    $max = $temp;
                    $start = $i + 1;
                    $end = $j;
                }
            }
        }
    }
    
    return substr($str,$start,$end - $start);
}

$str = "zXoDphpprogrammingDkmq";

$result = GetLongestSubstring($str);

echo $result;




/*
run:

phpprogramming

*/

 



answered Dec 25, 2022 by avibootz
...