How to insert spaces between words that start with capital in a string with PHP

1 Answer

0 votes
function insert_space(&$s) { 
    for ($i = 1; $i < strlen($s); $i++) {
         if ($s[$i] >= 'A' && $s[$i] <= 'Z') { 
             $s = substr($s, 0, $i) . " " . substr($s, $i);
             $i++;
         }
    }
    return $s;
} 



$s = "PythonJavaPascalC#C++F#"; 

insert_space($s);
        
echo $s;



    
/*
run:

Python Java Pascal C# C++ F#
         
*/

 



answered Jan 16, 2020 by avibootz
...