Jump to content

Getting post count


Martin18

Recommended Posts

I'm working on something and I need to be able to grab post count from another forum.

 

I looked into regex and I'm really clueless.

 

<div class='row1' style='padding:6px; margin-bottom:1px; padding-left:10px'>1,856 posts (14.15 per day)</div>

 

The posts are displayed like that.. how would I go about retrieving just the "1,856 posts (14.15 per day)" part.

 

I was thinking you could just get whatevers between <div class='row1' style='padding:6px; margin-bottom:1px; padding-left:10px'> and </div> but that's not the only text between html like that.

Link to comment
https://forums.phpfreaks.com/topic/187993-getting-post-count/
Share on other sites

One method (and is an alternative to regex) would be to make use of dom / domxpath like so:

 

$html = <<<EOF
   <div class='someCrap'>whatever</div>
   <div class='row1' style='padding:6px; margin-bottom:1px; padding-left:10px'>1,856 posts (14.15 per day)</div>
   <div class='someCrap'>whatever</div>
EOF;

$dom = new DOMDocument;
libxml_use_internal_errors(true);
@$dom->loadHTML($html); // for a live site, change @$dom->loadHTML to @$dom->loadHTMLFile and put url in quotes within the parenthesis
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
$divTag = $xpath->query('//div[@class = "row1"]');

foreach ($divTag as $val) {
    echo $val->nodeValue . "<br />\n";
}

//Output: 1,856 posts (14.15 per day)

 

EDIT: Granted, this makes some very rudimentary assumptions (like looking for a div with the class = "row1"). This may be too 'loose fisted' with regards to control. I guess it entirely depends on the html div possibilities. So this solution may (or may not) offer enough control to distinguish which divs exactly you are looking at.

Link to comment
https://forums.phpfreaks.com/topic/187993-getting-post-count/#findComment-992553
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.