Extract PHP numbers from strings

How can I add commas to numbers in PHP?
Convert number to month name in PHP

This solution will extract numbers from strings by ignoring 0s and separate into three string numbers.

solution 1.

$string = '02300103024000100';
$output = array();
// First we replace any '00...' string with just '0'
for($a=strlen($string); $a>0; $a--){
   $zero_replace = '';
   while(strlen($zero_replace) < $a) $zero_replace .= '0';
   $string = str_replace($zero_replace,'0',$string);
}
// Now we remove '0' from beginning and end of string (if they exist)
if(substr($string,0,1) == '0') $string = substr($string,1);
if(substr($string,-1,1) == '0') $string = substr($string,0,strlen($string)-1);
// Finally, explode by '0'
$output = explode('0',$string);
print_r($output);

Output

Array ( [0] => 23 [1] => 1 [2] => 3 [3] => 24 [4] => 1 )

How can I add commas to numbers in PHP?
Convert number to month name in PHP

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top