如何从PHP字符串中仅取出数字?

meh*_*mpt 8 php string numbers

假设,我有这个字符串:

$string = "Hello! 123 How Are You? 456";
Run Code Online (Sandbox Code Playgroud)

我想将变量设置$int$int = 123456;

我该怎么做?

例2:

$string = "12,456";
Run Code Online (Sandbox Code Playgroud)

需要:

$num = 12456;
Run Code Online (Sandbox Code Playgroud)

谢谢!

Iły*_*sov 29

正确的变体将是:

$string = "Hello! 123 How Are You? 456";
$int = intval(preg_replace('/[^0-9]+/', '', $string), 10);
Run Code Online (Sandbox Code Playgroud)