-2

I am trying to parse some code out of a div with a dynamic id, for example: (where xxxx stands for random numbers).

Is there any way to scrape given div's elements by using regex to match all the possible id, for example post-1111, post-1213, etc..

Here's my code:

dd`

    $target_html = $list_array[$i]->href;
    $ftp_html = file_get_html($target_html);

    $ftp_list = $ftp_html->find('div.main', 0);
    $ftp_array = $ftp_list->find('div#post-4885 a[id="player"]');
    for($j = 0; $j < sizeof($ftp_array); $j++){
      print_r($ftp_array[$j]->rel.'<br>');
    }
    echo '<hr>';

Example HTML on a server:

<div class="main>
<div id="post-xxxx:>
   elements...

  </div></div>`
Emma
  • 27,428
  • 11
  • 44
  • 69

1 Answers1

0

Sure is possible to scrape the TAG (only).

Here you go
(the post id's are in group 2, the entire tag is in group 0)

(?si)<div(?=(?:[^>"']|"[^"]*"|'[^']*')*?\sid\s*=\s*(?:(['"])\s*(post-\d+).*?\1))\s+(?:".*?"|'.*?'|[^>]*?)+>

https://regex101.com/r/VcWo2D/1

Readaable

 (?si)

 <div
 (?=
      (?: [^>"'] | " [^"]* " | ' [^']* ' )*?
      \s id \s* = \s* 
      (?:
           ( ['"] )                      # (1)
           \s* 
           ( post- \d+ )                 # (2)
           .*? 
           \1 
      )
 )
 \s+ 
 (?: " .*? " | ' .*? ' | [^>]*? )+
 >