-1

I want to get the two numbers in a string

Example: the user input "what is 177 x 55", then I want to get 177 and 55

$number1 = 177;
$number2 = 55;
Qirel
  • 25,449
  • 7
  • 45
  • 62

1 Answers1

0

You can use regex for this.

Regex will match and capture in strings according to a pattern, in this case the pattern is a simple any size digit.

preg_match_all("/(\d+)/", $string, $matches);
list($number1, $number2) = $matches[1];

The regex will capture any number of numbers in the string ("155 x 25 x 40" for example), but the list() function is the limiting factor here.
If there is three numbers in the list() will fail.
So I recommend you to use the array instead of the list($number1, $number2).

https://3v4l.org/DcAsX

Andreas
  • 23,610
  • 6
  • 30
  • 62