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


preg_match to get mp3 and mp4 filenames in file extension order then in filename order from an external webpage?

我是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";
}   

网页代码

70.123.0.111 -/lrs/35/

70.123.0.111 -/lrs/35/


[到父目录]

2008/7/17 21:02 178899264 0102.mp3 2008/7/18 12:上午 30 558244970 0102.mp4 2008/7/17 21:38 186016896 0304.mp3 2008/7/18 1:44 580476123 0304.mp4 2008/7/17 22:13 186478272 0506.mp3 2008/7/18 2:59 581455848 0506.mp4 2008/7/17 22:44 166862592 0708.mp3 2008/7/18 4:02 520139478 0708.mp4 2008/7/17 23:19 188616096 0910.mp3








2008/7/18 5:16 590308801 0910.mp4
2008/12/11 20:15 688347 qfm_hetu_vidya.pdf
2008/12/11 14:08 421978 ym_notes.pdf

我认为如果您是正则表达式的新手,那么 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";
}   

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

(使用 href 代替 herf)