How to extract only numbers from a string in PHP

1 Answer

0 votes
function extract_only_numbers($s) { 
    $len = strlen($s); 
    $numbers = "";
          
    for ($i = 0; $i < $len; $i++) { 
         $ch = $s[$i]; 
         if (ctype_digit($ch)) {
            $numbers = $numbers . $ch; 
         }
    } 
    return $numbers;
} 
  

$s = "c++14@vb.net&%java*() php <>/python 3.7.3"; 

echo extract_only_numbers($s);
     

 
   
/*
run:
        
14373
 
*/

 



answered Aug 15, 2019 by avibootz
...