Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to extract numbers from a string in PHP

3 Answers

0 votes
$s = "Today i like the number 31 and the number 13 tomorrow... maybe 100 :-)";

preg_match_all('!\d+!', $s, $numbers);

print_r($numbers);



/*
run:

Array ( [0] => Array ( [0] => 31 [1] => 13 [2] => 100 ) )

*/


answered Nov 5, 2014 by avibootz
edited Apr 13, 2024 by avibootz
0 votes
$s = '12php 36java20';
 
preg_match_all('!\d+!', $s, $num_matches);
 
print_r($num_matches[0]);
 
 
   
   
/*
run:
        
Array ( [0] => 12 [1] => 36 [2] => 20 ) 
       
*/

 



answered Apr 13, 2024 by avibootz
0 votes
$s = '12php 36java20 90 01';
  
preg_match_all('!\d+!', $s, $num_matches);
  
foreach ($num_matches[0] as $val) {
    echo $val . "\n";
}
  
    
    
/*
run:
         
12
36
20
90
01
        
*/

 



answered Apr 13, 2024 by avibootz

Related questions

2 answers 173 views
1 answer 177 views
1 answer 177 views
1 answer 148 views
1 answer 63 views
1 answer 62 views
...