首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用php提取html数据

用php提取html数据
EN

Stack Overflow用户
提问于 2015-07-12 09:31:36
回答 1查看 23关注 0票数 0

我有以下HTML (作为示例)

代码语言:javascript
复制
<span class="small margin-l5 left">
  <a  data-user-id="" class="showdataemployer">
    <span>
      (0 Reviews)
    </span>
 </a>
</span>

我想从(0条评论)中提取"0“

我定义了以下函数来抓取数据:

代码语言:javascript
复制
function scrape_between($data, $start, $end){
    $data = stristr($data, $start); // Stripping all data from before $start
    $data = substr($data, strlen($start));  // Stripping $start
    $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
    $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
    return $data;   // Returning the scraped data from the function
}

在本例中,我使用以下代码来尝试捕获0。

代码语言:javascript
复制
$reviews = scrape_between($projectPage,
"<a  data-user-id=\"\" class=\"showdataemployer\"><span>(",
"Reviews)</span>");

但到目前为止,我得到的回报是空白的。有什么想法吗?我猜大多数人都会推荐使用pregex来解决这个问题。但我似乎无法理解这一点。如果是这样的话,有没有人可以给我举一个例子,说明pregex是如何在这个特殊的例子中提取0的?

非常感谢你的帮助。谢谢你们。

EN

回答 1

Stack Overflow用户

发布于 2015-07-12 09:46:42

以下是使用简单的HTML DOM Parser http://simplehtmldom.sourceforge.net/manual.htm#section_traverse完成此操作的一种方法。

代码语言:javascript
复制
include_once 'simple_html_dom.php';
$html = str_get_html('<span class="small margin-l5 left">
  <a  data-user-id="" class="showdataemployer">
    <span>
      (0 Reviews)
    </span>
 </a>
</span>');
echo trim($html->find('span', 1)->plaintext);

输出:

(0条评论)

这不是PHP默认提供的,但可以在这里获得,http://simplehtmldom.sourceforge.net/。有关其他解析器的信息,请参阅此链接How do you parse and process HTML/XML in PHP?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31363502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档