【发布时间】:2014-06-25 20:43:51
【问题描述】:
url 中的 html sn-p (www.foo.com/index.html):
...
<th class="name" align="left" scope="col">
<a class="foo" href="foo.html">foo</a>
</th>
...
<th class="name" align="left" scope="col">
<a class="bar" href="bar.html">bar</a>
</th>
...
<th class="name" align="left" scope="col">
<a class="ba" href="baz.html">baz</a>
</th>
......
我想通过 php 获取 .name 类中的所有文本并将其转换为 JSON
所以结果是这样的:
{"names":["foo","bar","baz"]}
这是我尝试过的:
function linkExtractor($html){
$nameArr = array();
$doc = new DOMDocument();
$doc->loadHTML($html);
$names = //how do i get the elements?
foreach($names as $name) {
array_push($nameArr, $name);
}
return $imageArr;
}
echo json_encode(array("names" => linkExtractor($html)));
【问题讨论】:
-
你为什么不试试 jquery 呢?
-
@Dwza 不会工作,因为 html 没有被执行...
-
您通常使用 xpath 执行此操作。请在提问前使用搜索。
-
@hakre 怎么会是重复的?
-
@Maximilian:正是为此:
//how do i get the elements?在您的问题中。