-2

How can I preg_match until no more results is found?

I'm using file get contents

for example i am taking this url

$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) If > preg_match is true > add count = +1 in the url (go to https://www.testcompany.com/count=2 url) and check if preg match value true or not.

2) Loop If until preg_match is false.

i want to get the last url which is matched with preg match value.

Dharman
  • 30,962
  • 25
  • 85
  • 135
narendra
  • 9
  • 2
  • 11
  • have you looked at `array_pop` to get the last element of an array?> – Professor Abronsius May 10 '20 at 13:25
  • no for first url i will check if preg_match value is matched or not for next how can i check for remaining url's if preg_match value is there or not. how can i loop the url (adding +1 for count and check) to find preg_match value match in each url. i want to get last url which is matched with preg_match value – narendra May 10 '20 at 13:32

1 Answers1

-1

you can try something like this one:

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

:)

peakle
  • 149
  • 5