在找不到更多结果之前,我如何进行 preg_match?我正在使用文件获取内容例如我正在使用这个网址$updated_url = "https://www.testcompany.com/count=1";$html2 = file_get_contents($updated_url); preg_match_all('/<ul class="standard">(.*?)<\/ul>/s', $html2, $test);1) 如果 > preg_match 为真 > 在 url 中添加 count = +1(转到https://www.testcompany.com/count=2 url)并检查 preg 匹配值是否为真。2) 循环 If 直到 preg_match 为假。我想获取与 preg 匹配值匹配的最后一个 url。
1 回答

斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
你可以尝试这样的事情:
<?php
const URL_PATTERN = 'https://www.testcompany.com/count=%s';
$regex = '/<ul class="standard">(.*?)<\/ul>/m';
$previousMatchList = [];
$matchList = [];
for ($count = 1; ; $count++) {
$html2 = file_get_contents(sprintf(URL_PATTERN, $count));
preg_match_all($regex, $html2, $matchList);
if (!array_filter($matchList)) {
break;
}
$previousMatchList = $matchList;
}
$lastValue = array_pop($previousMatchList[0]);
var_dump($lastValue);
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报
0/150
提交
取消