clock  Mon - Sun 8.00 AM - 8.00 PM
fb
instagram
play store
pinterest

How to find numbers from string in PHP

writter  Hariom Prajapati
Date  24 Jun 2022
Language  PHP
How to find numbers from string in PHP

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

php