How to get substring that start and end with the same character in PHP

1 Answer

0 votes
function char_substr($str, $ch, &$start, &$end) {  
    for ($i = 0; $str[$i] != '\0'; $i++) {  
        if ($str[$i] == $ch) {
            $start = $i;
            for ($j = $i + 1; $str[$j] != '\0'; $j++) {
                if ($str[$j] == $ch) {  
                    $end = $j; 
                    break;
                }
            }
        }
        if ($start != 0) {
            break;
        }
    }
}  
   
 
$str = "hgdabcvauyec"; 
$start = 0;
$end = 0;
     
char_substr($str, 'a', $start, $end);
      
for ($i = $start; $i <= $end; $i++) {
      echo $str[$i];
}
     
 
 
 
/*
run:
      
abcva
     
*/

 



answered Apr 18, 2019 by avibootz

Related questions

...