0

I have this block of html code and I'm trying to parse the contents out of the div with the "points" "stat-label". I have done this for the div with the "stat-label" amount and it works perfectly.

preg_match('#\$[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?#', $xx1, $output1);
$parts1 = $output1[0];
$val1 = trim(str_replace('$','',$parts1));
$value1= preg_replace('/[\$,]/', '', $val1);

But I can't get it to read the value of "points". Any ideas??

Tried this:

preg_match('/^\\d+(\\.\\d+)?$/D', $xx1, $output2);

and the result was:

object(DOMNodeList)#7 (1) {
  ["length"]=>
  int(0)
}


<div class="widget">
    <div class="widget-header">

        <!-- content -->
    </div>

    <div class="widget-content">

        <div class="stat">
            <div class="stat-header">
                <div class="stat-label">
                    <!-- content -->
                </div>
                <div class="stat-value">
                    <!-- content -->
                </div>
            </div>
        </div>
        <hr>

        <div class="stat">
            <div class="stat-header">
                <div class="stat-label">
                    <!-- content -->
                </div>
                <div class="stat-value">
                    <!-- content -->
                </div>
            </div>
        </div>
        <hr>

        <div class="stat">
            <div class="stat-header">
                <div class="stat-label">
                    <!-- content -->
                </div>
                <div class="stat-value">
                    <!-- content -->
                </div>
            </div>
        </div>
        <hr>

        <div class="stat">
            <div class="stat-header">
                <div class="stat-label">
                    Amount
                </div>
                <div class="stat-value">
                    <font color="green">$</font>123,456,678,012 </div>
            </div>
        </div>
        <hr>

        <div class="stat">
            <div class="stat-header">
                <div class="stat-label">
                    Points
                </div>
                <div class="stat-value">
                    12.14 </div>
            </div>
        </div>
        <hr>

        <div class="stat">
            <div class="stat-header">
                <div class="stat-label">
                    <!-- content -->
                </div>

                <div class="stat-value">
                    <!-- content -->
                </div>
            </div>
        </div>
        <hr>

        <div class="stat">
            <div class="stat-header">
                <div class="stat-label">
                    <!-- content -->
                </div>
                <div class="stat-value">
                    <!-- content -->
                </div>
            </div>
        </div>
    </div>
</div>
wiZZmnma
  • 59
  • 6
  • 1
    "[Have you tried using an XML parser instead?](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)" – Scimonster Dec 01 '14 at 09:36
  • 1
    Oh read this one, you will love it: [golden link](http://stackoverflow.com/a/1732454/2663825) – Dexa Dec 01 '14 at 09:37
  • @Dexa okay, I now feel like I've been using a screwdriver to trim my beard..lol I full understand my approach was/is wrong. Please direct me to any better methods since I'm new to all of this. – wiZZmnma Dec 01 '14 at 10:50

2 Answers2

0

12.14 is enclosed by whitespaces your RE is not expecting. Either trim() it before, or don't use ^$

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
0

so, after looking at PHP's dom parsing potential, I have abandoned all instances where I used regex to parse html.

Here is how I solved the question above:

<?php
$login_data=  http_build_query(array('username'=>$username,'password'=>$password));
$html = _curl("http://example.com/getinfo.php",'POST',$login_data); // this is a curl function I use

$dom = new DOMDocument();
$dom->loadHTML($html);
$els = $dom->getElementsByTagName('*');
$child = 0;
$myAmount = 0;
foreach ( $els as $el ) {
    $firstChild = $el->firstChild;
    $child++;
if($child == "96"){   // this was the firstChild that has the amount data
$myAmount = trim($firstChild->wholeText);
}

}

echo $myAmount; // outputs 12.14! 

?>

So, its answered for me at least. please refer to the links in the comments above.

wiZZmnma
  • 59
  • 6