[Solved] Perl to PHP equivalent: extract strings with regex


This code will do as you ask. It uses preg_match_all as simbabque described

<?php

$html="<tr class="aaa"><td class="bbb">221.86.2.163</td><td>443</td><td><div><span class="ccc"></span> example <span> example</span></div></td></tr><tr class="aaa"><td class="bbb">221.86.2.163</td><td>443</td><td><div><span class="ccc"></span> example <span> example</span></div></td></tr>";

preg_match_all('|td class="bbb">([\d.]+)</td><td>(\d+)</td>|', $html, $out, PREG_SET_ORDER);

foreach ( $out as $item ) {
    echo "$item[1]:$item[2]\n";
}

?>

output

221.86.2.163:443
221.86.2.163:443

9

solved Perl to PHP equivalent: extract strings with regex