0

我是 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/


[到父目录]

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.mp
2008 凌晨 4:02 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 qfya._m
2008 年 11 月2:08 下午 421978 ym_notes.pdf

4

2 回答 2

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";
}   
于 2012-04-12T02:40:46.317 回答
0

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

(使用 href 代替 herf)

于 2013-10-30T19:14:32.083 回答