.Maui Blog

Extract numbers from a string with php

Often, you only need to extract numbers from a string. PHP makes life easy with the filter_var function used with FILTER_SANITIZE_NUMBER_INT.
That’s how:

<?php

$string = 'Sambuca has about 40 degrees';
$only_numbers = filter_var($string , FILTER_SANITIZE_NUMBER_INT);
echo $only_numbers; // 40
Exit mobile version