How to convert part of a string between two indexes to uppercase in PHP

1 Answer

0 votes
function convert_part_to_uppercase($s, $from_idx, $to_idx) { 
    $len = strlen($s);
    
    if ($from_idx < 0 || $to_idx > $len) {
        return s;
    }
    
    for ($i = 0; $i < $len; $i++) {
         if (($i >= $from_idx && $i <= $to_idx) && ($s[$i] >= 'a' && $s[$i] <= 'z')) {
              $s[$i] = strtoupper($s[$i]);
         }
    }
    return $s;
} 
   
$s = "php programming";
  
$s = convert_part_to_uppercase($s, 3, 6);
echo $s . "<br />"; 

$s = convert_part_to_uppercase($s, 11, 12);
echo $s . "<br />"; 
    
   
           
    
/*
run:
                
php PROgramming
php PROgramMIng
         
*/

 



answered Nov 19, 2019 by avibootz
...