Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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_Regex_Arrays - Fatal编程技术网

Php 正则表达式搜索帮助

Php 正则表达式搜索帮助,php,regex,arrays,Php,Regex,Arrays,给定一个字符串,例如: a:2:{i:0;s:1:"1";i:1;s:1:"2";} 我想找到引号中的每个整数,并创建一个包含字符串中所有整数的数组 最终结果应该是如下所示的数组: Array ( [0] => 1 [1] => 2 ) 我猜您使用preg_match(),但我没有使用正则表达式的经验:(这个怎么样: $str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}'; print_r(array_values(unserializ

给定一个字符串,例如:

a:2:{i:0;s:1:"1";i:1;s:1:"2";}
我想找到引号中的每个整数,并创建一个包含字符串中所有整数的数组

最终结果应该是如下所示的数组:

Array
(
    [0] => 1
    [1] => 2
)
我猜您使用preg_match(),但我没有使用正则表达式的经验:(

这个怎么样:

 $str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
 print_r(array_values(unserialize($str)));
不是正则表达式,答案是一样的

这是因为您拥有的字符串是一个序列化的PHP数组。使用正则表达式将是错误的做法。

这样做如何:

 $str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
 print_r(array_values(unserialize($str)));
不是正则表达式,答案是一样的

这是因为您拥有的字符串是一个序列化的PHP数组。使用正则表达式是错误的做法。

正则表达式(在程序中)如下所示:

$str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
preg_match_all('/"(\d+)"/', $str, $matches);
print_r($matches[1]);
正则表达式(在程序中)如下所示:

$str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
preg_match_all('/"(\d+)"/', $str, $matches);
print_r($matches[1]);