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/


[親ディレクトリへ]

2008/7/17 21:02 178899264 0102.mp3
2008/7/18 12:30 AM 558244970 0102.mp4
2008/7/17 21:38 186016896 0304.mp3
2008/7/18 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 7
/8/ 1mp3 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 qya_fm_pdf
1 2008 年 11月 2:08 PM 421978 ym_notes.pdf

4

2 に答える 2

1

正規表現を初めて使用する場合は、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

コードの 2 行目に問題がある可能性があります。

(herf の代わりに href を使用)

于 2013-10-30T19:14:32.083 に答える