preg_match_all只返回第一个匹配
问题描述:
好吧,所以基本上我试图通过大量的html代码,其中包含超链接朝着文件。我正在使用preg_match_all来查找所有的事件。但是,它永远不会返回预期的比赛数量。preg_match_all只返回第一个匹配
射击HTML代码($的含量值):
<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代码:
preg_match_all('/download\.php\?f=(.*?)">/', $content, $matches);
foreach($matches as $val){
echo $val[0] ."<br />";
}
只有上面的代码返回的第一场比赛对我来说。回复:
echo $val[1] ."<br />"; //Returns 2nd match
echo $val[2] ."<br />"; //Returns 3rd match
//etc
所以我想我应该只是计数数组并包装它在for循环解决这个问题。但是:
count($matches); //Returns 1
答
首先,你应该carefuly阅读php.net文档http://php.net/manual/en/function.preg-match-all.php
但在简历,preg_match_all投入$使用取决于标志的结果一致:在默认情况下PREG_PATTERN_ORDER所以$匹配阵列应该是
对结果排序使得$ matches [0]是满图案 匹配的数组,$匹配[1]的由前 括号内的子模式所匹配的字符串数组,等等。
你的情况:
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
)
)
所以,如果你想列出所有的结果,你可以做
foreach($matches[0] as $val){
echo $val ."<br />";
}
+0
谢谢,虽然这返回字符串“download.php”以及。显然我需要使用$ matches [1]来获得我想要的。 – icecub 2014-10-30 15:07:23
答
你的模式是正确的,但你在错误的地方寻找
当我倾倒你的结果,我发现它是确定:
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"
}
}
你应该永远* *解析HTML与正则表达式。改为使用[PHP DOM](http://php.net/manual/en/book.dom.php)。 – 2014-10-30 14:15:48
'foreach($ matches [1] as $ val){' – VolkerK 2014-10-30 14:23:12
@VolkerK非常感谢。这正是我所需要的:) – icecub 2014-10-30 15:05:35