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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.