How to Extract Numbers from String in PHP

  • Tech Area
  • November 3, 2023


In this tutorial, We will see how to extract the numbers from the string using the PHP inbuilt function. We are using filter_var() function to filters a variable with the FILTER_SANITIZE_NUMBER_INT. This function is used to both validate and sanitize the data.

Solution

<?php
$string = 'Welcome 2 the Tech Area';
$number = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
echo $number;
?>

Output

2


Subscribe us via Email

Join 10,000+ subscriber


SHORT QUERIES
How to Extract Numbers from String in PHP Last Updated: November 3, 2023
How to Replace Space with Underscore in PHP Last Updated: November 3, 2023
How to Concatenate two or more string in PHP Last Updated: October 24, 2023
How to get Yesterday and Tomorrow Date in PHP Last Updated: October 24, 2023
View all