在找不到更多結果之前,我如何進行 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 關注
- 130 瀏覽
添加回答
舉報
0/150
提交
取消