我有以下HTML (作为示例)
<span class="small margin-l5 left">
<a data-user-id="" class="showdataemployer">
<span>
(0 Reviews)
</span>
</a>
</span>我想从(0条评论)中提取"0“
我定义了以下函数来抓取数据:
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。
$reviews = scrape_between($projectPage,
"<a data-user-id=\"\" class=\"showdataemployer\"><span>(",
"Reviews)</span>");但到目前为止,我得到的回报是空白的。有什么想法吗?我猜大多数人都会推荐使用pregex来解决这个问题。但我似乎无法理解这一点。如果是这样的话,有没有人可以给我举一个例子,说明pregex是如何在这个特殊的例子中提取0的?
非常感谢你的帮助。谢谢你们。
发布于 2015-07-12 09:46:42
以下是使用简单的HTML DOM Parser http://simplehtmldom.sourceforge.net/manual.htm#section_traverse完成此操作的一种方法。
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?
https://stackoverflow.com/questions/31363502
复制相似问题