[英]preg_match - extract only numbers
我有一个类似Results 1 - 20 of 400
的字符串。
考虑到这个例子,我需要得到1
, 20
和400
,以3个不同的变量。
我正在尝试此代码,但未成功:
preg_match('/\d{1,}/', $text, $array);
这应该为您工作:
$text = "Results 1 - 20 of 400";
preg_match_all('/\d+/', $text, $array);
//^^^^ Note here! 'preg_match_all' not 'preg_match' to get all matches! (Your regex would also work '/\d{1,}/')
print_r($array);
输出:
Array ( [0] => Array ( [0] => 1 [1] => 20 [2] => 400 ) )
有关preg_match_all()
更多信息,请参见手册: http : //php.net/manual/en/function.preg-match-all.php
从那里引用:
preg_match_all —执行全局正则表达式匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.