首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >preg_match_all只返回第一次匹配

preg_match_all只返回第一次匹配
EN

Stack Overflow用户
提问于 2014-10-30 14:14:51
回答 2查看 2.3K关注 0票数 0

好的,基本上,我正在尝试浏览大量的html代码,其中包含指向文件的超链接。我使用preg_match_all查找所有事件。但是,它永远不会返回预期的匹配量。

快照HTML代码($content值):

代码语言:javascript
复制
<a class="file_download file_ext_docx" href="/download.php?f=/LiO2beoordeling%20door%20mentor%20Maartje%20ingevuld.docx">Download file 1.docx</a><br /><em>Some text<a class="file_download file_ext_docx" href="/download.php?f=/BP3/Referenties.docx">Download file 2.docx</a> </strong><br /><strong>- Some text: <a class="file_download file_ext_docx" href="/download.php?f=/Zelfevaluatie%204.2.docx">Download file 3.docx</a> Soem text: <a class="file_download file_ext_docx" href="/download.php?f=/BP3/sz-lio.docx">Download file 4</a> </strong><br /><a class="file_download file_ext_docx" href="/download.php?f=/BP3/poplio.docx">

PHP代码:

代码语言:javascript
复制
preg_match_all('/download\.php\?f=(.*?)">/', $content, $matches);
foreach($matches as $val){
    echo $val[0] ."<br />";
}

上面的代码只返回我的第一次匹配。奇怪的是,回音:

代码语言:javascript
复制
echo $val[1] ."<br />"; //Returns 2nd match
echo $val[2] ."<br />"; //Returns 3rd match
//etc

所以我想我应该数一下数组,然后把它封装在一个for循环中来解决这个问题。然而:

代码语言:javascript
复制
count($matches); //Returns 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-30 14:46:33

首先,您应该仔细阅读php.net文档http://php.net/manual/en/function.preg-match-all.php

但是在简历中,preg_match_all根据您使用的标志: PREG_PATTERN_ORDER,默认情况下将结果放入$matches,因此$matches数组应该是

Orders的结果使得$matches是一个完全模式匹配的数组,$matches1是由第一个括号大小的子模式匹配的字符串数组,依此类推。

就你而言:

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [0] => download.php?f=/LiO2beoordeling%20door%20mentor%20Maartje%20ingevuld.docx">
            [1] => download.php?f=/BP3/Referenties.docx">
            [2] => download.php?f=/Zelfevaluatie%204.2.docx">
            [3] => download.php?f=/BP3/sz-lio.docx">
            [4] => download.php?f=/BP3/poplio.docx">
        )

    [1] => Array
        (
            [0] => /LiO2beoordeling%20door%20mentor%20Maartje%20ingevuld.docx
            [1] => /BP3/Referenties.docx
            [2] => /Zelfevaluatie%204.2.docx
            [3] => /BP3/sz-lio.docx
            [4] => /BP3/poplio.docx
        )

)

所以如果你想列出所有的结果,你可以

代码语言:javascript
复制
foreach($matches[0] as $val){
    echo $val ."<br />";
}
票数 2
EN

Stack Overflow用户

发布于 2014-10-30 14:35:12

你的模式是对的,但你看错地方了。

当我抛出你的结果时,我发现它没问题:

代码语言:javascript
复制
array(2) {
  [0]=>
  array(5) {
    [0]=>
    string(75) "download.php?f=/LiO2beoordeling%20door%20mentor%20Maartje%20ingevuld.docx">"
    [1]=>
    string(38) "download.php?f=/BP3/Referenties.docx">"
    [2]=>
    string(42) "download.php?f=/Zelfevaluatie%204.2.docx">"
    [3]=>
    string(33) "download.php?f=/BP3/sz-lio.docx">"
    [4]=>
    string(33) "download.php?f=/BP3/poplio.docx">"
  }
  [1]=>
  array(5) {
    [0]=>
    string(58) "/LiO2beoordeling%20door%20mentor%20Maartje%20ingevuld.docx"
    [1]=>
    string(21) "/BP3/Referenties.docx"
    [2]=>
    string(25) "/Zelfevaluatie%204.2.docx"
    [3]=>
    string(16) "/BP3/sz-lio.docx"
    [4]=>
    string(16) "/BP3/poplio.docx"
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26655374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档