How to find integer in string php?

Since there is only 1 numeric value to isolate in your string, I would endorse and personally use filter_var() with FILTER_SANITIZE_NUMBER_INT.

echo filter_var($string, FILTER_SANITIZE_NUMBER_INT);

A whackier technique which works because there is only 1 numeric value AND the only characters that come before the integer are letters, colons, or spaces is to use ltrim() with a character mask then cast the remaining string as an integer.

Demo

$string = "In My Cart : 11 items";
echo (int)ltrim($string, 'A..z: ');
// 11

If for some reason there was more than one integer value and you wanted to grab the first one, then regex would be a direct technique.

Demo

echo preg_match('/\d+/', $string, $m) ? $m[0] : '';

sscanf() is rather handy if you need to explicitly cast the numeric string as an integer (or float). If it is possible/unknown for the integer value to occur at the start of the string, then prepend a non-numeric character to the input string before scanning it. The following technique matches leading non-digits (and ignores them with * after the %), then matches the first occurring sequence of digits and casts the returned substring as an integer.

Demo

var_dump(sscanf(' ' . $string, '%*[^0-9]%d')[0]);

To adapt this technique to extract a float value, just change the d to f. For more information on the (currently undocumented) assignment suppression feature of sscanf(), see this post.

In this article, we will extract numbers from strings using PHP. There are various built-in methods to extract numbers from strings, some of them are discussed below.

Methods:

  • Using preg_match_all() Function.
  • Using  filter_var() Function.
  • Using  preg_replace() function.

Method 1: Using preg_match_all() Function.

Note:  preg_match() function is used to extract numbers from a string. Usually, the search starts from the beginning of the subject string. The optional parameter offset is used to specify the position from where to start the search.

Syntax:

int preg_match( $pattern, $input, $matches, $flags, $offset )

Return value: It returns true if a pattern exists, otherwise false.

Example 1: Thefollowing exampleextracts Integer Numbers using the preg_match_all() function.

PHP

$geeks = 'Welcome 2 Geeks 4 Geeks.';

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

print_r($matches);

?>

Output

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 4
        )

)

Example 2: The following example extracts decimal numbers using the preg_match_all() function.

PHP

$geeks = 'Welcome 2 Geeks 4.8 Geeks.';

preg_match_all('!\d+\.*\d*!', $geeks, $matches);

print_r($matches);

?>

Output

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 4.8
        )

)

Method 2: Using filter_var() function.

Note: The filter_var() function filters a variable with the specified filter. This function is used to both validate and sanitize the data.

Syntax:

filter_var(var, filtername, options)

Return Value: It returns the filtered data on success, or FALSE on failure.

Example:

PHP

$geeks = 'Welcome 2 Geeks 4 Geeks.';

$int_var = (int)filter_var($geeks, FILTER_SANITIZE_NUMBER_INT);

echo ("The numbers are: $int_var \n");

?>

Output

The numbers are: 24 

Method 3: Using  preg_replace() function.

Note: The preg_replace() function is an inbuilt function in PHP that is used to perform a regular expression for search and replace the content.

Syntax:

preg_replace( $pattern, $replacement, $subject, $limit, $count )

Return Value: This function returns an array if the subject parameter is an array, or a string otherwise.

Example:

PHP

   $geeks = 'Welcome 2 Geeks 4 Geeks.';

  $int_var = preg_replace('/[^0-9]/', '', $geeks);  

   echo("The numbers are: $int_var \n"); 

?>

Output

The numbers are: 24 


How check string is integer in PHP?

The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.

How can I get integer value in PHP?

Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0).
is_int().
is_integer() - alias of is_int().
is_long() - alias of is_int().

How do you get a specific value from a string in PHP?

Answer: Use the PHP substr() function The PHP substr() function can be used to get the substring i.e. the part of a string from a string. This function takes the start and length parameters to return the portion of string.

How extract all numbers from string in PHP?

We can use the preg_replace() function for the extraction of numbers from the string.