15

Suppose, I have this string:

$string = "Hello! 123 How Are You? 456";

I want to set variable $int to $int = 123456;

How do I do it?

Example 2:

$string = "12,456";

Required:

$num = 12456;

Thank you!

4
  • 5
    $int = preg_replace('/\D+/', '', $string); Commented Mar 29, 2014 at 7:24
  • possible duplicate of Extract numbers from a string Commented Mar 29, 2014 at 7:25
  • 1
    It's not a duplicate of that question, which is about getting all/any (groups of) numbers. Commented Mar 29, 2014 at 7:25
  • Here is the solution-stackoverflow.com/questions/11243447/… Commented Mar 29, 2014 at 7:39

5 Answers 5

39

Correct variant will be:

$string = "Hello! 123 How Are You? 456";
$int = intval(preg_replace('/[^0-9]+/', '', $string), 10);
Sign up to request clarification or add additional context in comments.

5 Comments

Now you check your code for string "hello 0123"... :)
@ArifHussain yes, it works for 0123 and provides proper decimal 123, as expected
@Lashane , so in that case Shankar's code is also correct, which you commented on.
This code will malform large numbers, for example abc12345678901234556782312312323123123131232
@mike php by nature cannot work with such big numbers, you need special library for this
4

You can use this method to select only digit present in your text

function returnDecimal($text) {
    $tmp = "";  
    for($text as $key => $val) {
      if($val >= 0 && $val <= 9){
         $tmp .= $val
      }
    }
    return $tmp;
}

1 Comment

Inefficient and slow.
2

Use this regular expression !\d!

<?php
$string = "Hello! 123 How Are You? 456";
preg_match_all('!\d!', $string, $matches);
echo (int)implode('',$matches[0]);

enter image description here

5 Comments

A 0 before a number is not required.
Shankar, I think what Lashane is getting at is that (int) will cast that as octal if it's prefaced by a 0. Or am I completely off?
@Helpful you're right, you cannot just use (int) to convert string to number in php
@Lashane, You are not getting it, the regex grabs the numbers from the string , and casting it to int is not going to making it as octal.
@ShankarDamodaran yes, I've mistaken with javascript where you 0 prefixed numbers will be supposed as octal
2

You can use the below:

$array = [];

preg_match_all('/-?\d+(?:\.\d+)?+/', $string, $array);

Where $string is the entered string and $array is where each number(not digit!,including also negative values!) is loaded and available for different more operations.

Comments

-1
<?php
    $string = "ABC100";
    preg_match_all('!\d+!', $string, $matches);
    $number = $matches[0][0];
    echo $number;
?>

Output: 100

1 Comment

This code-only answer provides no new value to this page or Stack Overflow. Please only answer when you have something unique and valuable to add to the page. Worse, it only displays the first of all matches.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.