【问题标题】:Should I use regex to parse this string of html table data?我应该使用正则表达式来解析这个 html 表数据字符串吗?
【发布时间】:2010-09-22 01:41:08
【问题描述】:

解析这些数据的最佳方法是什么?我应该使用正则表达式还是其他东西?数据是 html 格式,但我是从一个网站上找到的,并且会解析这个并且只有这个字符串(注意:字符串要长得多 - 超过 1,300 个实例 - 下面只有两个) - 注意我使用 php 和 jquery 进行大多数 Web 编程。

我只需要提取第二个 td 中的数据,并且只提取锚内的锚文本 - 在实例 1 中,它是:Best,Jahvid DET RB

我只需要运行这个循环一次。

<tr class="oddtablerow"><td class="rank">1.</td><td class="rank">1.</td><td class="player"><a href="http://football22.myfantasyleague.com/2010/player?L=34793&amp;P=9839"  title="Salary: $2250000, Year: 3, Status: 3, Info: Drafted 10 1:04 Team, Week 3: at Vikings Sun 1:00 p.m. ET" class="position_rb">Best, Jahvid DET RB</a> (R) </td><td class="points tot">53.90</td><td class="points avg">26.950</td><td class="points"><a href="detailed?L=34793&amp;W=1&amp;P=9839&amp;YEAR=2010">17.55</a></td> 
<td class="points"><a href="detailed?L=34793&amp;W=2&amp;P=9839&amp;YEAR=2010">36.35</a></td> 
<td class="status"><a title="Owner: William Gold"  class="franchise_0009" href="http://football22.myfantasyleague.com/2010/options?L=34793&amp;F=0009&amp;O=01">Team Name</a> - <a href="options?L=34793&amp;O=05&amp;FRANCHISE=0013,0009&amp;PLAYER=9839,">Trade</a></td><td class="week">7</td><td class="salary">$2250000</td></tr> 
<tr class="eventablerow myfranchise "><td class="rank">2.</td><td class="rank">2.</td><td class="player"><a href="http://football22.myfantasyleague.com/2010/player?L=34793&amp;P=3291"  title="Salary: $7400000, Year: 3, Status: 3, Info: , Week 3: at Broncos Sun 4:15 p.m. ET" class="position_qb">Manning, Peyton IND QB</a></td><td class="points tot">49.61</td><td class="points avg">24.805</td><td class="points"><a href="detailed?L=34793&amp;W=1&amp;P=3291&amp;YEAR=2010">26.66</a></td> 
<td class="points"><a href="detailed?L=34793&amp;W=2&amp;P=3291&amp;YEAR=2010">22.95</a></td> 
<td class="status"><a title="Owner: Robert M. Cavezza "  class="myfranchise franchise_0013" href="http://football22.myfantasyleague.com/2010/options?L=34793&amp;F=0013&amp;O=01">The Bullies</a></td><td class="week">7</td><td class="salary">$7400000</td></tr> 

编辑:jquery 的答案怎么了?我正要实现它,但它消失了

【问题讨论】:

  • 正则表达式和 HTML?您在正确的网站上:)
  • 这可能是网站上最多的upvoted answer。您最好使用正确的 html parser 并从中提取信息。
  • 有没有办法查看被管理员或作者删除的旧答案?
  • 被作者删除了,jQuery和PHP完全没有关系,这大概就是被删除的原因吧。
  • 废话 - 找不到缓存的答案?

标签: php parsing html-parsing php-parser


【解决方案1】:

如果您正在寻找执行速度最快的解决方案,XmlReader 是最快的 XML 解析器之一。使用起来有点困难,然后是DOM等其他解决方案,但是由于要解析大量条目,因此性能可能很重要。

否则DOM 使用起来非常简单。您可以在我在另一个问题上给出的this answer 中找到如何使用的简单示例。

如果你想将你的内容作为字符串加载,你可以这样做:

XMLReader

$foo = new XMLReader();
$foo->xml($yourStringHere);

DOMDocument

$foo = new DOMDocument();
$foo->loadHTML($yourStringHere);

【讨论】:

  • 要使用这些xml阅读器,是否需要将这些数据转换为xml对象,然后解析代码?或者我可以使用这些数据作为 php 字符串来解析 xml 吗?
  • 我将网站上的 html 复制并粘贴到字符串中 - 所以该字符串被视为 xml 还是我必须构建一个 domdocument? - 在您的示例中,您使用 domdocument 和一个 html 文件 - 我将使用一个 html 字符串,我应该将引用的文本放在 get_file_contents 函数位于该脚本中的位置吗?
  • @Bob 查看我的编辑,了解如何将数据作为字符串加载到 XMLReader 和 DOMDocument 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2019-12-10
相关资源
最近更新 更多