搜索文本块,使用PHP返回MP3链接 [英] Search block of text, return MP3 links using PHP

查看:70
本文介绍了搜索文本块,使用PHP返回MP3链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最新的项目中,使用PHP时遇到了一些麻烦.基本上,我有一段文本($text),我想搜索该文本并返回所有MP3链接.我知道它与正则表达式有关,但我无法使其正常工作.

I've just run into a little bit of trouble with some PHP on my latest project. Basically I have a block of text ($text) and I would like to search through that text and return all of the MP3 links. I know it has something to do with regular expressions but I just cannot get it working.

这是我当前的代码:

    if(preg_match_all(".mp3", $text, $matches, PREG_SET_ORDER)) {

  foreach($matches as $match) {
   echo $match[2];
   echo $text;
        }
    }

推荐答案

再一次,正则表达式在解析HTML方面表现极差.使用适当的HTML解析器将信息从网页中抓取.

Once again, regex is extremely poor at parsing HTML. Use a proper HTML parser to scrape information out of a web page.

例如,使用 DOMDocument::loadHTML() 来解析HTML内容,然后 getElementsByTagName('a') 以获得列表中的链接页.对于每个链接,getAttribute('href')可以查看其指向的位置.

For example, use DOMDocument::loadHTML() to parse the HTML content, then getElementsByTagName('a') to get a list of links in the page. For each link, getAttribute('href') to see where it points.

但是请注意,绝对不能保证MP3文件将始终且仅存储在以.mp3结尾的文件名下.在网络上,资源的类型不必来自文件扩展名.唯一确定确定URL指向的文件类型的唯一方法是继续进行获取(使用HTTP GET或HEAD请求).

Note however that there is absolutely no guarantee that MP3 files will always and only be stored under filenames ending in .mp3. On the web, the type of a resource does not have to come from a file extension. The only way to find out for sure what type of file a URL points to is to go ahead and fetch it (with an HTTP GET or HEAD request).

这篇关于搜索文本块,使用PHP返回MP3链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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