teckfolls teckfolls - 12 days ago 6
PHP Question

PHP preg_match_all search in preg_match_all (2 times)

I'm not good in English.

I'm trying to preg_match in a preg_match!

I got this:

if( preg_match_all('~<td class="fluctuation">\s*(.*?)\s*</td>~si', $input, $item_updown_select ) );


if I var_dump it, I get this:

array(2) {
[0]=>
array(32) {
[0]=>
string(153) "<td class="fluctuation">
<span class="down"><span class="icon"></span>13.31%</span>
</td>"
[1]=>
string(150) "<td class="fluctuation">
<span class="up"><span class="icon"></span>3.45%</span>
</td>"
[2]=>
string(150) "<td class="fluctuation">
<span class="up"><span class="icon"></span>4.56%</span>
</td>"
[3]=>
string(151) "<td class="fluctuation">
<span class="up"><span class="icon"></span>10.07%</span>
</td>"

}
[1]=>
array(32) {
[0]=>
string(58) "<span class="down"><span class="icon"></span>13.31%</span>"
[1]=>
string(55) "<span class="up"><span class="icon"></span>3.45%</span>"
[2]=>
string(55) "<span class="up"><span class="icon"></span>4.56%</span>"
[3]=>
string(56) "<span class="up"><span class="icon"></span>10.07%</span>"
}
}


now I want from this array only the class!

I know it works if I do:

if( preg_match_all('~<span class="\s*(.*?)\s*"><span class~si', $item_updown_select[1][0], $item_updown0 ) );
if( preg_match_all('~<span class="\s*(.*?)\s*"><span class~si', $item_updown_select[1][1], $item_updown1 ) );
if( preg_match_all('~<span class="\s*(.*?)\s*"><span class~si', $item_updown_select[1][2], $item_updown2 ) );
if( preg_match_all('~<span class="\s*(.*?)\s*"><span class~si', $item_updown_select[1][3], $item_updown3 ) );


But if I got more than 3 Items, thats bad coding!

You know a better way?

I'm google it now for about 8 hours and dont got somethink.

I already tryed foreach:

foreach($item_updown_select[1] as $index => $text_to_draw) {
if( preg_match_all('~<span class="\s*(.*?)\s*"><span class~si', $text_to_draw, $item_updown ) );
print_r( $item_updown );
}


and thats my output:

Array
(
[0] => Array
(
[0] => <span class="down"><span class
)

[1] => Array
(
[0] => down
)

)
Array
(
[0] => Array
(
[0] => <span class="up"><span class
)

[1] => Array
(
[0] => up
)

)
Array
(
[0] => Array
(
[0] => <span class="down"><span class
)

[1] => Array
(
[0] => down
)

)


My problem on foreach is: How I can display now every array separately?

I dont know if foreach is the right methode to do this.

I hope you understand me and know what I want.

I wish you can help me!!

Answer

Normally, I would recommend against using Regular Expressions to parse HTML, as noted in a previous reply.

However, for this rather limited use I think you might get away with it. Do be mindful of the fact that if the underlying HTML changes, or is different from the examples above, this will will not work (anymore)

// If you _only_ need the class:
$pattern = '~<td class="fluctuation">\s*<span class="([^"]+).*?</td>~si';

// If you need the class as well:
$pattern = '~<td class="fluctuation">\s*(<span class="([^"]+).*?)\s*</td>~si';

if( preg_match_all($pattern, $input, $item_updown_select ) );

The first RegEx will give you only the class $matches[1], while the second will give you the classes in $matches[2].

I would like to close with a strong recommendation to learn DOMdocument as it makes working with HTML a lot easier, and more change-tolerant. You can start with the following tutorial.

Comments