Search block of text, return MP3 links using PHP

自作多情 提交于 2019-12-25 01:50:46

问题


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.

Here's my current code:

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

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

回答1:


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

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.

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).



来源:https://stackoverflow.com/questions/3514520/search-block-of-text-return-mp3-links-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!