【发布时间】:2017-08-06 09:08:26
【问题描述】:
我构建了这个函数来从一个 html 页面获取 h1 标签:
//$html = file_get_html('https://www.sports-reference.com/olympics/summer/1896/');
//echo $html;
function getTextBetweenTags($url, $tagname) {
$values = array();
$html = file_get_html($url);
foreach($html->find($tagname) as $tag) {
$values[] = trim($tag->innertext);
}
return $values;
}
$output = getTextBetweenTags('https://www.sports-reference.com/olympics/summer/1896/', 'h1');
echo '<pre>';
print_r($output);
作为输出我得到:
Array
(
[0] => 1896 Athina Summer Games
)
是否可以代替:
Array
(
[0] => 1896
[1] => Athina
[2] => Summer
)
很好接受的其他解决方案,因为我确定 h1 标签是页面中唯一的标签,所以我不需要从 html 中查找所有 h1 标签
【问题讨论】:
-
games怎么样?你不能在太空爆炸吗? -
你为什么在你的期望结果数组中省略
Games?是因为您不想包含Games这个词吗?还是因为您不希望包含最后一句话?还是因为您专门搜索year、location、season?
标签: php