Preg_match值为多个url的url数[重复]。

问题描述 投票:-1回答:1

怎样才能做到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.comcount=2 url)并检查preg匹配值是否为真。

2) 循环If,直到preg_match为false。

我想得到最后一个与preg匹配值匹配的url。

php arrays for-loop preg-match
1个回答
-1
投票

你可以试试像这样的方法。

<?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);

:)

© www.soinside.com 2019 - 2024. All rights reserved.