1

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 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 know what's wrong, why it won't work?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kyle
  • 3,004
  • 15
  • 52
  • 79

3 Answers3

3

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.

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
0

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/

  • Hmm.. How would I do that? I've never messed with nodes or XML parsing:( – Kyle Sep 04 '10 at 20:32
  • Read the SimpleCoder's solution above. You can get a glimpse on how to do that on the webpage. –  Sep 04 '10 at 20:34
0

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.

Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828