简体   繁体   English

在PHP中从表中获取数据

[英]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" 我正在尝试查找具有“ toplist_body_link”匹配项的表并显示为“ 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. 代替正则表达式,使用PHP库进行DOM操作。 I believe I have used this one before: http://simplehtmldom.sourceforge.net/ . 我相信我以前曾经用过这个: http : //simplehtmldom.sourceforge.net/ Very simple to use. 使用非常简单。 Because this is not XML, PHP DOM will probably not work for you. 因为这不是XML,所以PHP DOM可能对您不起作用。

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). 如果是xHTML,则建议使用PHP XML解析器对其进行解析,然后使用节点而不是正则表达式访问数据。 Usually regex is a bad idea to parse html/xhmtl. 通常,正则表达式是解析html / xhmtl的坏主意。

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

SimpleCode above is right, use DOM parser: http://simplehtmldom.sourceforge.net/ 上面的SimpleCode是正确的,请使用DOM解析器: 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 . 这个问题以及有关使用正则表达式解析HTML的所有问题都以史诗般的方式得到了解答,这是RegEx匹配开放标签(XHTML自包含标签除外)的最佳答案。 Required reading. 必读。

Also see the blog by fearless leader. 另请参阅无畏领袖的博客

Don't use regexp, use a real parsing solution. 不要使用regexp,请使用真正的解析解决方案。 If your HTML is valid XML/XHTML, use DOM , or XSLTProcessor . 如果您的HTML是有效的XML / XHTML,请使用DOMXSLTProcessor If you can't depend on it being valid XHTML, use Beautiful Soup or the SimpleHtmlDom package that @SimpleCoder references. 如果您不能依靠它为有效的XHTML,请使用Beautiful Soup或@SimpleCoder引用的SimpleHtmlDom包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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