tr0in tr0in - 4 months ago 19
Perl Question

Perl to PHP equivalent: extract strings with regex

What is the PHP equivalent for this Perl code?

my $html = '<tr class="aaa"><td class="bbb">111.111.111.111</td><td>443</td><td><div><span class="ccc"></span> example <span> example</span></div></td></tr><tr class="aaa"><td class="bbb">222.222.222.222</td><td>443</td><td><div><span class="ccc"></span> example <span> example</span></div></td></tr>';

print "$1:$2\n" while $html =~ /class="aaa"><td class="bbb">(.*?)<\/td><td>(\d+)<\/td>/g;


I tried with this code, but it gives infinite loop.

while(preg_match('/td class=\"bbb\">(.*?)<\/td><td>(\d+)<\/td>/',$html,$out)) {
echo "$out[1]:$out[2]\n";
}


Also, with
if
instead of
while
it gives only one result.

Expected output (IP:PORT):

111.111.111.111:443
222.222.222.222:443


Environment: Windows 7 with PHP 5.5.12 (WAMP v2.5).

Answer

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
Comments