2010-01-06 63 views
1

我想匹配網頁上的Rapidshare鏈接序列。該鏈接看起來像:匹配Rapidshare鏈接與正則表達式

http://rapidshare.com/files/326251387/file_name.rar 

我寫了這個代碼:

if(preg_match_all('/http:\/\/\rapidshare\.com\/files\/.*?\/.*?/', $links[1], $links)) 
{ 
    echo 'Found links.'; 
} else { 
    die('Cannot find links :('); 
} 

它retuns每次Cannot find links :(。請注意,我想返回整個匹配項,所以它會返回數組中頁面上找到的每個Rapidshare鏈接。

$links[1]也有一個有效的字符串。

任何幫助將不勝感激,歡呼聲。

回答

1

看起來你有免費電子

前流浪反斜線
if(preg_match_all('/http:\/\/\rapidshare\.com\/files\/.*?\/.*?/', $links[1], $links)) 

應該

if(preg_match_all('/http:\/\/rapidshare\.com\/files\/.*?\/[^\s"']+/', $links[1], $links)) 

\r是一個回車符)

+0

謝謝,愚蠢的錯別字:)但只是一件事,現在它重新鏈接數組,如:'http:// rapidshare.com/files/328807106 /',例如不是文件名? – Matt 2010-01-06 00:05:58

+0

只返回第一個字符,但謝謝。得到它的工作:) – Matt 2010-01-06 00:15:23

+0

更新它以匹配最後的文件名。當它看到空白,一個「或」字符時會停止(假設你試圖將數據從href屬性中拉出來...... – mopoke 2010-01-06 00:16:14

0

爲避免你進入那個瘋狂在URL中跳出斜線,我會用另一個delimiter作爲我的正則表達式 - 例如#;這將有助於看到你在rapideshare之前有太多的\


然後,你可以有一些看起來像這樣:
(從你的靈感 - 只在最後改了一下,因爲它沒有返回文件名;;你可能要適應這種多一點,不過,exlclude一些其他人物不僅僅是白色空間,像"

$str = 'blah http://rapidshare.com/files/326251387/file_name.rar blah'; 
if(preg_match_all('#http://rapidshare\.com/files/(.*?)/([^\s]+)#', $str, $m)) { 
    var_dump($m); 
} 


其中,在這裏,將讓你:

array 
    0 => 
    array 
     0 => string 'http://rapidshare.com/files/326251387/file_name.rar' (length=51) 
    1 => 
    array 
     0 => string '326251387' (length=9) 
    2 => 
    array 
     0 => string 'file_name.rar' (length=13) 
1

有很多的nonlogic HTTPS鏈接rapidshare.com,你可以找到他們在谷歌: 「https://rapidshare.com/files/」

我建議改變你的正則表達式包含https?: