PHP Get Only Numbers from String Example
Hi Dev,
This tutorial shows you php get only numbers from string example. We will look at example of how to get only numbers from string in php?. We will look at example of get only numbers from string in php example. you will learn how to use function to get only numbers from string in php?.
There are tow example to get only numbers from string in PHP. in this example, we will use to preg_replace() and preg_match_all() function to Get Only Numbers. so Let's both the following example with output.
Example 1: Get Only Numbers of a String
index.php
<?php
$string = 'Our company has 3 owners and 10 employees';
// get only numbers of a string
$result = preg_replace('/[^0-9]/',' ', $string);
echo $result;
?>
Output:
3 10
Example 2: Get Only Numbers of a String
index.php
<?php
$str = "There are 11 players in cricket team and 12 players in football team";
// get only numbers of a string
preg_match_all('!\d+!', $str, $matches);
$number = implode(',', $matches[0]);
echo $number;
?>
Output:
11,12
I hope it could help you...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example