Estrai i numeri PHP dalle stringhe

Come posso aggiungere virgole ai numeri in PHP?
Converti il ​​numero in nome del mese in PHP

Questa soluzione estrarrà i numeri dalle stringhe ignorando gli 0 e separandoli in tre numeri di stringa.

soluzione 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);

produzione

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

Come posso aggiungere virgole ai numeri in PHP?
Converti il ​​numero in nome del mese in PHP

Rimani aggiornato su PHP!

Non spammiamo!

en English
X
Scorrere fino a Top