溫馨提示×

php怎么提取字符串中的數字

PHP
小億
140
2024-01-02 14:36:40
欄目: 編程語言

可以使用正則表達式來提取字符串中的數字,例如使用preg_match_all函數來匹配所有的數字:

$str = "Hello123World456";
preg_match_all('/\d+/', $str, $matches);
$numbers = $matches[0];

print_r($numbers);

輸出結果:

Array
(
    [0] => 123
    [1] => 456
)

在上述代碼中,使用正則表達式/\d+/匹配連續的數字,然后使用preg_match_all函數將所有匹配結果保存在$matches變量中。最后將提取到的數字打印出來。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女