简体   繁体   中英

Grabbing data from table in PHP

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?

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.

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM