Preg_match_all 返回数组内的数组? [英] Preg_match_all returning array within array?

查看:80
本文介绍了Preg_match_all 返回数组内的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这个数组中获取信息,但由于某种原因,它把所有内容都嵌套到了 $matches[0] 中.

I am trying to get the information out of this array, but for some reason it is nesting everything into $matches[0].

<?

$file = shell_exec('pdf2txt.py docs/April.pdf');

preg_match_all('/.../',$file,&$matches);
print_r($matches)

?>

这是否按预期工作?有没有办法将其放入深度为 1 的数组中?

Is this working as intended? Is there a way to put this in an array of depth 1?

这是正则表达式:

([A-Z][a-z]+\s){1,5}\s?[^a-zA-Z\d\s:,.\'\"]\s?[A-Za-z+\W]+\s[\d]{1,2}\s[A-Z][a-z]+\s[\d]{4}

推荐答案

preg_match_all() 总是返回一个数组(如果成功,否则你得到一个空数组)其中索引 0 包含一个数组,每个匹配项都有一个元素,其他索引成为捕获组,每个匹配项都有一个内部数组.

preg_match_all() always returns an array (if successful, otherwise you get an empty array) where index 0 contains an array with an element for each entire match, and the other indexes become the capturing groups, with an internal array for each match.

这可能更容易理解...

This might be easier to understand...

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(12) "entire match"
    [1]=>
    string(32) "entire match matched second time"
  }
  [1]=>
  array(2) {
    [0]=>
    string(15) "capturing group"
    [1]=>
    string(35) "capturing group matched second time"
  }
}

这篇关于Preg_match_all 返回数组内的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆