views:

38

answers:

3

So far this is what I have to work with:

   <div class="toplist">
                <div class="toplist_left"></div>
                <div class-"toplist_body">
                <div class="toplist_right"></div>
                 <div class="toplist_body_rank">9</div>
                 <div class="toplist_body_link"><a href="?support=details&id=204">


Gunz Reloaded &nbsp;&nbsp;&nbsp;<font size=1 color=#d4d2cf>Online</font></small>

</a></div>
                 <div class="toplist_desc">27 7 || DDoS Protection || Hacks</div>
                 <div class="toplist_votes">5665</div>
             </div>
             </div>

I'm trying to find the table with the "toplist_body_link match and display it's "toplist_votes"

Do you guys know how I could do this?

I tried this:

<?php
$topsite = file_get_contents('[removed link]');

preg_match(('#<div class=\"toplist_body_votes\">(.*)#', $topsite, $match) && preg_match('#<a href=\"?support=details&id=204\">#'));
$votes = $match[1];

echo "Current Votes: $votes \n";
?>

Do you guys know what's wrong, why it won't work?

A: 

If it is xHTML then I would suggest parsing it using PHP XML parser and then accessing data using the nodes instead of regex(s). Usually regex is a bad idea to parse html/xhmtl.

http://php.net/manual/en/book.xml.php

SimpleCode above is right, use DOM parser: http://simplehtmldom.sourceforge.net/

AJ
Hmm.. How would I do that? I've never messed with nodes or XML parsing:(
Kyle
Read the SimpleCoder's solution above. You can get a glimpse on how to do that on the webpage.
AJ
+3  A: 

Instead of Regular Expressions, use a PHP library for DOM manipulation. I believe I have used this one before: http://simplehtmldom.sourceforge.net/. Very simple to use. Because this is not XML, PHP DOM will probably not work for you.

SimpleCoder
Wow this worked perfectly:) I'll update what I find!
Kyle
Good, I'm glad I could help!
SimpleCoder
A: 

This question and all questions about parsing HTML with regular expressions have been answered in epic fashion in the top answer for RegEx match open tags except XHTML self-contained tags. Required reading.

Also see the blog by fearless leader.

Don't use regexp, use a real parsing solution. If your HTML is valid XML/XHTML, use DOM, or XSLTProcessor. If you can't depend on it being valid XHTML, use Beautiful Soup or the SimpleHtmlDom package that @SimpleCoder references.

Bill Karwin