简体   繁体   English

preg_match_all仅返回第一个匹配项

[英]preg_match_all only returns first match

Ok so basically I'm trying to walk through a large amount of html code that contains hyperlinks towards files. 好的,所以基本上我正在尝试遍历包含指向文件的超链接的大量html代码。 I'm using preg_match_all to find all occurrences. 我正在使用preg_match_all查找所有出现的事件。 However, it's never returning the amount of matches expected. 但是,它永远不会返回预期的匹配量。

Shot HTML code (value of $content): 镜头HTML代码($ content的值):

<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 code: PHP代码:

preg_match_all('/download\.php\?f=(.*?)">/', $content, $matches);
foreach($matches as $val){
    echo $val[0] ."<br />";
}

The code above only returns the first match for me. 上面的代码只为我返回第一个匹配项。 Strangely enough, echoing: 奇怪的是,呼应:

echo $val[1] ."<br />"; //Returns 2nd match
echo $val[2] ."<br />"; //Returns 3rd match
//etc

So I figured I should just count the array and wrap it around in a for loop to solve this problem. 因此,我想我应该对数组进行计数并将其包装在for循环中以解决此问题。 However: 然而:

count($matches); //Returns 1

First, you should read carefuly the php.net documentation http://php.net/manual/en/function.preg-match-all.php 首先,您应该仔细阅读php.net文档http://php.net/manual/en/function.preg-match-all.php

But in resume, preg_match_all put in $matches the results depending the flag you use: PREG_PATTERN_ORDER by default so the $matches array should be 但是在简历中,preg_match_all会根据您使用的标志将$ matches结果放入结果中:PREG_PATTERN_ORDER默认情况下,因此$ matches数组应为

Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. 对结果进行排序,以使$ matches [0]是完整模式匹配的数组,$ matches [1]是由第一个带括号的子模式匹配的字符串的数组,依此类推。

in your case: 在您的情况下:

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
        )

)

So if you want to list all the results, you can just do 因此,如果要列出所有结果,则可以执行

foreach($matches[0] as $val){
    echo $val ."<br />";
}

Your pattern is right, but you look in the wrong place 模式正确,但位置错误
when i dump your result i found it ok: 当我转储您的结果时,我发现还可以:

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"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM