preg_match以文件扩展名顺序获取mp3和mp4文件名,然后从外部网页按文件名顺序获取?

时间:2012-04-12 02:22:37

标签: php

我是PHP的新手。我已经尝试了很多次,并且无法提出搜索模式来从外部HTML获取所有mp3和mp4文件名。文件列表的文件扩展名顺序不是文件名顺序。如果有任何帮助,我将不胜感激。

$file = 'http://70.123.0.111//lrs/35/';
$searchfor = '<a herf';
echo $searchfor;

// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);

// escape special characters in the query
$pattern = preg_quote($searchfor, '/');

// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/";

// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found";
}   

HTML代码

70.123.0.111 - /lrs/35/

70.123.0.111 - / lrs / 35 /


[To Parent Directory]

7/17/2008 9:02 PM 178899264 0102.mp3
7/18/2008 12:30 AM 558244970 0102.mp4
7/17/2008 9:38 PM 186016896 0304.mp3
7/18/2008 1:44 AM 580476123 0304.mp4
7/17/2008 10:13 PM 186478272 0506.mp3
7/18/2008 2:59 AM 581455848 0506.mp4
7/17/2008 10:44 PM 166862592 0708.mp3
7/18/2008 4:02 AM 520139478 0708.mp4
7/17/2008 11:19 PM 188616096 0910.mp3
7/18/2008 5:16 AM 590308801 0910.mp4
12/11/2008 8:15 PM 688347 qfm_hetu_vidya.pdf
12/11/2008 2:08 PM 421978 ym_notes.pdf

2 个答案:

答案 0 :(得分:1)

我认为如果你是regex的新手,那么www.rubular.com应该是你的新朋友。

假设文本文档总是像你一样,并且文件名中没有任何空格,那么这个正则表达式将执行:“/(\ w + .mp [34])/”

下面的代码对我有用(不确定$ searchfor变量的用途)。希望这会有所帮助。

$file = 'test.html';
//$searchfor = '<a herf';
//echo $searchfor;

// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);

// finalise the regular expression, matching the whole line
$pattern = "/(\w+\.mp[34])/";

// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found";
}   

答案 1 :(得分:0)

也许您的问题出现在代码的第二行: $ searchfor ='herf';

(使用href代替herf)