Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将所有整数提取到一个数组中_Php - Fatal编程技术网

PHP将所有整数提取到一个数组中

PHP将所有整数提取到一个数组中,php,Php,我有以下字符串: 15 asdas 26 dasda 354 dasd 1 我要做的就是把所有的数字都提取到一个数组中,它看起来是这样的: array(4) { [0]=> string(2) "15" [1]=> string(2) "26" [2]=> string(3) "354" [3]=> string(1) "1" } PHP中有什么方法可以实现它吗?您可以使用: 您可以使用: 看到这个了吗 解决方案1: 此解决方案使用:

我有以下字符串:

15 asdas 26 dasda 354 dasd 1
我要做的就是把所有的数字都提取到一个数组中,它看起来是这样的:

array(4) {
  [0]=>
  string(2) "15"
  [1]=>
  string(2) "26"
  [2]=>
  string(3) "354"
  [3]=>
  string(1) "1"
}
PHP中有什么方法可以实现它吗?

您可以使用:

您可以使用:

看到这个了吗

解决方案1: 此解决方案使用:

解决方案2: 此解决方案使用您自己的功能:

function is_number($var) { return !(0 == intval($var)); }
print_r(array_filter(split(" ", "15 asdas 26 dasda 354 dasd 1"),"is_number"));
解决方案3: 对于5.3.0及更高版本,此解决方案使用:

看到这个了吗

解决方案1: 此解决方案使用:

解决方案2: 此解决方案使用您自己的功能:

function is_number($var) { return !(0 == intval($var)); }
print_r(array_filter(split(" ", "15 asdas 26 dasda 354 dasd 1"),"is_number"));
解决方案3: 对于5.3.0及更高版本,此解决方案使用:

使用预匹配:

检查站点底部的示例。

使用预匹配:


查看站点底部的示例。

split()函数不推荐使用。感谢Gergo Erdosi看到这一点。Praveen Kumar:我也是,我有+2/-1
split()
函数不推荐使用。感谢Gergo Erdosi看到这一点。Praveen Kumar:我也是,我有+2/-1
function is_number($var) { return !(0 == intval($var)); }
print_r(array_filter(split(" ", "15 asdas 26 dasda 354 dasd 1"),"is_number"));
print_r(array_filter(preg_split(" ", "15 asdas 26 dasda 354 dasd 1"),"is_numeric"));
$str = '15 asdas 26 dasda 354 dasd 1';
preg_match_all('/\d+/', $str, $matches);
print_r($matches);