我有一个php文件,可以读取另一个网站上的html文档,并将数据存储在名为“$ content”的变量中
$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
我无法弄清楚是从$ content / html获取此文本字符串的最佳方法
<b>111 players online</b>
我打算尝试在 标签之间获取文字,但它是一个相当大的html文件,并且有数百个标签
然后我研究了使用html DOM,但无法弄清楚如何确定elementId或tagName
任何帮助将不胜感激
哦,对于任何想知道网站的人我试图让玩家在线发送文字是 -view-source:http://www.pkhonor.net/
答案 0 :(得分:2)
PHP DOMDocument是您在此处想要使用的内容,特别是用于将HTML解析为对象的loadHTML方法。然后,您可以将所有<b>
元素捕获到一个数组中,然后可以循环遍历:
$doc = new DOMDocument();
$doc->loadHTML($content);
$bolds = $doc->getElementsByTagName('b');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
echo $node->nodeValue;
}
}