數組給定一個字符串:如何在字符串中發現號碼存儲到在PHP
$string = "Hello, 1992 world! Today's 2016! That's 24 years!";
我如何解析字符串返回一個包含裏面所有的數字數組?
預期結果:
{1992,2016,24}
數組給定一個字符串:如何在字符串中發現號碼存儲到在PHP
$string = "Hello, 1992 world! Today's 2016! That's 24 years!";
我如何解析字符串返回一個包含裏面所有的數字數組?
預期結果:
{1992,2016,24}
使用preg_match_all()
就可以實現你want-
$str = "Hello, 1992 world! Today's 2016! That's 24 years!";
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
http://stackoverflow.com/questions/6278296 /提取號碼從 - 一個串 – mboyde
$str = 'Hello, 1992 world! Today's 2016! That's 24 years!';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
嘗試'preg_match_all什麼()' –
的http:// PHP .net/manual/en/function.preg-match-all.php – mboyde