If you just want to filter everything other than the numbers out, the easiest is to use filter_var:

$str = 'In My Cart : 11 items';
$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
Answer from Daniel Bøndergaard on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-extract-numbers-from-string-in-php
How to extract numbers from string in PHP ? - GeeksforGeeks
July 23, 2025 - ... <?php // Sample string with numbers $string = "The order numbers are 123, 456, and 789."; // Using preg_match_all to extract numbers preg_match_all('/\d+/', $string, $matches); // Extracted numbers $numbers = $matches[0]; // Print the extracted ...
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-find-number-from-string-in-phpexample.html
How to Find Number from String in PHP? - ItSolutionstuff.com
May 14, 2024 - So, let's follow few step to create example of php take number from string. Here, i will give you simple example how to get number value from string content in php.
🌐
Delft Stack
delftstack.com › home › howto › php › how to extract numbers from a string in php
How to Extract Numbers From a String in PHP | Delft Stack
February 2, 2024 - The program below shows how we can use the preg_match_all() function to extract numbers from a given string. <?php $string = 'Sarah has 4 dolls and 6 bunnies.'; preg_match_all('!\d+!', $string, $matches); print_r($matches); ?>
🌐
PHP
php.net › manual › en › function.intval.php
PHP: intval - Manual
The solution for this, and the way I recommend converting a string to an integer, is: $num = $num + 0; and PHP will leave your number alone; it'll just know it's a number. Such is the fun of a loosely-typed language.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-extract-numbers-from-a-string-in-php
How to extract Numbers From a String in PHP ? - GeeksforGeeks
July 23, 2025 - We can use the preg_replace() function for the extraction of numbers from the string. /[^0-9]/ pattern is used for finding number as integer in the string (Refer to Example 1) /[^0-9\.]/ pattern is used for finding number as double in the string ...
🌐
Java2Blog
java2blog.com › home › php › extract numbers from string in php
Extract Numbers From String in PHP [3 ways] - Java2Blog
December 4, 2022 - Use preg_match_all() method with /[0-9]+/ to extract numbers from String in PHP.
Find elsewhere
🌐
Techie Delight
techiedelight.com › home › java › extract numbers from a string in php
Extract numbers from a string in PHP | Techie Delight
July 7, 2026 - This article demonstrates how to extract numbers from a string in PHP. The idea is to identify all non-numeric characters in a string, and replace them with an empty string (""). In PHP, this can be done using the preg_replace() function.
🌐
Uptimia
uptimia.com › home › questions › how to extract a single integer from a string?
How To Extract A Single Integer From A String?
November 18, 2024 - To extract all integers from a ... $numbers = $matches[0]; // Array of all matched integers · PHP's filter_var function extracts numbers from strings....
🌐
Useotools
useotools.com › blog › 2 › how-to-extract-number-from-string-using-php
How to extract number from string using PHP - Useotools
October 6, 2023 - $string = "There are 15 Oranges and 3 Bananas."; $numbers = preg_replace('/D/', '', $string); echo $numbers; ... The regular expression /D/ matches any non-digit character. The preg_replace() function replaces all non-digit characters with an ...
🌐
Krayonnz
krayonnz.com › user › doubts › detail › 63d6776e671433004f25f5d8 › how-to-extract-numbers-from-a-string-in-php
How to extract Numbers From a String in PHP?
The fastest growing social learning network of students. Participate in quizzes and competitions to win cash & exciting prizes. Host your college competitions & quizzes. Buy & Sell verified notes. Ask & answer doubts.
🌐
Sentry
sentry.io › sentry answers › php › how do i convert a string to a number in php?
PHP Convert String to Number with Casting and intval | Sentry
July 31, 2026 - Convert numeric strings to integers or floats in PHP using dynamic typing, intval(), floatval(), (int)/(float) casting, or the settype() function
🌐
Snipplr
snipplr.com › view › 71658 › php--extract-numbers-from-a-string
PHP - Extract numbers from a string - PHP Snipplr Social Repository
June 28, 2013 - A PHP function to return the first N words from a string · 1707 · Extract Numbers from a string · 1793 · Remove a character from string within a range · 1864 · Get parameter from an URI string in PHP · 2910 · PHP regex - Remove special characters from a string ·
🌐
GitHub
gist.github.com › lerua83 › 7448987
PHP - Extract numbers from a string: URL: http://stackoverflow.com/questions/6278296/extract-numbers-from-a-string I want to extract the numbers from a string that contains numbers and letters like · GitHub
November 13, 2013 - PHP - Extract numbers from a string: URL: http://stackoverflow.com/questions/6278296/extract-numbers-from-a-string I want to extract the numbers from a string that contains numbers and letters like - extractNumbersString.php
🌐
W3Schools
w3schools.com › php › php_numbers.asp
PHP Numbers
A float is a number with a decimal point or a number in exponential form: 2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats. ... The float data type can commonly store a value up to 1.7976931348623E+308 (platform dependent), and have a maximum precision of 14 digits. PHP has the following predefined constants for floats (from PHP 7.2):
🌐
SitePoint
sitepoint.com › php
Split number string and get required numbers - PHP - SitePoint Forums | Web Development & Design Community
October 28, 2013 - ... Yep, put a limit match on it. $string = 'my description has 123456789 numbers'; $number = preg_replace("/[^0-9]{1,4}/", '', $string); // return 1234 ... I am getting out as 123456789 instead 1234.