好的,基本上,我正在尝试浏览大量的html代码,其中包含指向文件的超链接。我使用preg_match_all查找所有事件。但是,它永远不会返回预期的匹配量。
快照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代码:
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发布于 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是由第一个括号大小的子模式匹配的字符串数组,依此类推。
就你而言:
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 />";
}发布于 2014-10-30 14:35:12
你的模式是对的,但你看错地方了。
当我抛出你的结果时,我发现它没问题:
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"
}
}https://stackoverflow.com/questions/26655374
复制相似问题