你好,我正在寻找一些html并将其显示在另一个页面上。我正在做一个广播服务器,我想从7.html抓取歌曲标题,但所有我需要的是歌曲的标题。只是一个注意,我已经尝试过fsockopen,但我的网站主机阻止了这一点,所以我不能连接和抓取歌曲标题那样。
HTML的源代码看起来像这样
<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>0,1,0,5000,0,96,SONG TITLE HERE</body></html>
我只需要显示"SONG TITLE HERE"我如何将这些信息设置为php变量
我知道下面的方法,但这里的问题是,它将得到一切。我只需要一部分源html。
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
<?php
$doc = new DOMDocument;
$doc->loadhtmlfile('http://www.example.com/');
$body = $doc->getElementsByTagName('body');
$content = $body->item(0)->textContent;
$arr = explode(',', $content);
$song = end($arr);
?>
试试这样:
<?php
$content = file_get_contenst("7.html");
$dom = new DOMDocument();
$dom->loadHTML($content);
$body = $dom->getElementsByTagName('body');
$info = $body->item(0)->nodeValue;
$infoArr = str_getcsv($info);
$title = array_pop($infoArr);
?>
有很多方法来处理这个。你也可以这样写:
preg_match("/<body>(.*?)<'/body>/i", $html, $matches);
代替DOMDocument。或者用explosion()代替str_getcsv()
我能够使用iframe从服务器的7.html中获取所有内容
我的主机不允许我以这种方式连接到其他服务器。