2016-10-04 64 views
-1

數組給定一個字符串:如何在字符串中發現號碼存儲到在PHP

$string = "Hello, 1992 world! Today's 2016! That's 24 years!"; 

我如何解析字符串返回一個包含裏面所有的數字數組?

預期結果:

{1992,2016,24} 
+1

嘗試'preg_match_all什麼()' –

+1

的http:// PHP .net/manual/en/function.preg-match-all.php – mboyde

回答

2

使用preg_match_all()就可以實現你want-

$str = "Hello, 1992 world! Today's 2016! That's 24 years!"; 
preg_match_all('!\d+!', $str, $matches); 
print_r($matches); 

Online Example

+0

http://stackoverflow.com/questions/6278296 /提取號碼從 - 一個串 – mboyde

1
$str = 'Hello, 1992 world! Today's 2016! That's 24 years!'; 
preg_match_all('!\d+!', $str, $matches); 
print_r($matches); 
相關問題