2

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!!

teckfolls
  • 37
  • 6

2 Answers2

0

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.

Community
  • 1
  • 1
ChristianF
  • 2,068
  • 9
  • 14
0

Use DOMDocument to parse HTML:

$html = <<<EOD
  <table>
    <tr>
      <td class="fluctuation">
        <span class="down"><span class="icon"></span>13.31%</span>
      </td>
      <td class="fluctuation">
        <span class="up"><span class="icon"></span>3.45%</span>
      </td>
      <td class="fluctuation">
        <span class="up"><span class="icon"></span>4.56%</span>
      </td>
      <td class="fluctuation">
        <span class="up"><span class="icon"></span>10.07%</span>
      </td>
    </tr>
  </table>
EOD;

$dom = new DOMDocument();
$dom->loadHTML($html);
$finder = new DomXPath($dom);
$nodes = $finder->query("//td[@class='fluctuation']");

$classes = array();
foreach($nodes as $node) {
    foreach($finder->query("span", $node) as $span) {
        $class = $span->getAttribute('class');
        $classes[] = $class;
    }

}
print_r($classes);

Output:

Array
(
    [0] => down
    [1] => up
    [2] => up
    [3] => up
)
Toto
  • 89,455
  • 62
  • 89
  • 125