How to convert part of a string to uppercase start from specific index in PHP

1 Answer

0 votes
function convert_part_to_uppercase($s, $idx) { 
    $len = strlen($s);
    
    if ($idx < 0 || $idx > $len) {
        return s;
    }
    
    for ($i = 0; $i < $len; $i++) {
         if ($i >= $idx && ($s[$i] >= 'a' && $s[$i] <= 'z')) {
             $s[$i] = strtoupper($s[$i]);
         }
    }
    return $s;
} 
   
$s = "php programming";
  
$s = convert_part_to_uppercase($s, 5);
 
echo $s; 
    
   
           
    
/*
run:
                
php pROGRAMMING
         
*/

 



answered Nov 19, 2019 by avibootz
edited Nov 19, 2019 by avibootz
...