How to find numbers from string in PHP


In this tutorial we will learn that how to find numbers from string in PHP.


Here we will use preg_match_all() function to get numbers from string in PHP.

The preg_match_all() function returns the number of matches according to the pattern which we created.


Syntax –

preg_match_all(pattern, input, matches, flags, offset)

 

Now follow below code to get numbers from string in PHP.


<?php 

  $string = 'You have 500 rupees and 13 paise remaining in your card ending with 3785';

    preg_match_all('!\d+!'$string, $matches);

    echo "<pre>";

  print_r($matches['0'])

?> 


Output

image


Related Topics