URL搜索带有file_get_contents的字符串 [英] URL Search for a string with file_get_contents

查看:62
本文介绍了URL搜索带有file_get_contents的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个PHP脚本,该脚本在URL( http://mp3skull.com/)中搜索文本希望将".mp3"作为字符串打印到歌曲的下载链接.下面是我到目前为止的代码,它在mp3skull页面的html代码中搜索".mp3",并通知用户是否成功.我需要帮助的部分:脚本需要能够在网页源中找到".mp3",然后在源代码中以纯文本字符串的形式显示其之前的url/文本.我希望这是有道理的,并且您能够为我提供帮助.谢谢.

I am writing a php script that searches a url (http://mp3skull.com/) for the text ".mp3" in hopes of printing the download link to a song as a string. Below is the code that I have so far, it searches the html code of a mp3skull page for ".mp3" and notifies the user if it is successful. The part I need help with: the script needs to be able to find ".mp3" in the webpage source, then print the url/text that comes before it in the source code as a string of plain text. I hope this makes sense and you are able to help me. Thanks.

<?php
$filename = 'http://mp3skull.com/mp3/hot_mallets.html';
$searchfor = '.mp3';
$file = file_get_contents($filename);
if(strpos($file, $searchfor)) 
{
   echo "String found";
}
?>

推荐答案

您为此工作使用了错误的工具.为了有效地获取这些值,请改用 DOMDocument + DOMXpath .示例:

You're using the wrong tool for that job. To effectively get those values, use DOMDocument + DOMXpath instead. Example:

$contents = file_get_contents('http://mp3skull.com/mp3/hot_mallets.html');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$element = $xpath->query('//div[@id="right_song"]/div[3]/div[1]/div[1]/a')->item(0)->getAttribute('href');
echo $element; //http://incoming.jazz-on-line.com/a/mp3a/VIC041408.mp3

这篇关于URL搜索带有file_get_contents的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆