Martin18 Posted January 11, 2010 Share Posted January 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187993-getting-post-count/ Share on other sites More sharing options...
nrg_alpha Posted January 11, 2010 Share Posted January 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187993-getting-post-count/#findComment-992553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.