Skip to content
WJunction - Webmaster Forum

Extract links

Status
Not open for further replies.
Hi, I'm writing 1 script that take backup of my rapidshare links..

But I have 1 problem: I can't extract from posts ID Links and Filename.

I use that:

Code:
<?php

$url = file_get_contents("http://www.domain.com");
$url = strip_tags( $url );
$regex = '@^https?://(?:[\w\d]+\.)*rapidshare\.\w+/files/(\d+)/([^&?#/]+?)(?:$|[&?#/])@i';
if(preg_match(
 $regex,$url, $matches)) {
 $file_url = $matches[0];
$file_id = $matches[1];
$file_name = $matches[2];
}

but I can only get entire link, like https://rapidshare.com/files/3220584445/test.zip

Instead, if I write only the link like in this case:

Code:
<?php

$url = 'https://rapidshare.com/files/3220584445/test.zip';
$regex = '@^https?://(?:[\w\d]+\.)*rapidshare\.\w+/files/(\d+)/([^&?#/]+?)(?:$|[&?#/])@i';
if(preg_match(
 $regex,$url, $matches)) {
  
   $file_url = $matches[0];
$file_id = $matches[1];
$file_name = $matches[2];
}
?>

I get:

Code:
Array
(
    [0] => https://rapidshare.com/files/3220584445/test.zip
    [1] => 3220584445
    [2] => test.zip



How can I extract only:
3220584445
test.zip
from a html page?

Thanks
 
Last edited:

6 comments

PHP:
<?php 
$url = 'https://rapidshare.com/files/322058444511/test.zip'; 
$regex = '@^https?://(?:[\w\d]+\.)*rapidshare\.\w+/files/(\d+)/([^&?#/]+?)(?:$|[&?#/])@i'; 
preg_match($regex, $url, $matches);  
/* Array (    
[0] => https://rapidshare.com/files/322058444511/test.zip     
[1] => 322058444511    
[2] => test.zip ) 
*/  
$file_url = $matches[0]; $file_id = $matches[1]; $file_name = $matches[2]; echo  $file_url . "\n" . $file_id . "\n" . $file_name . "\n";
 
Explode?

PHP:
<?php  

print_r(explode('/','https://www.rapidshare.com/files/265645559/some.file.rar'));  

?>


Result:
Code:
Array (     
[0] => https:     
[1] =>      
[2] => www.rapidshare.com     
[3] => files     
[4] => 265645559     
[5] => some.file.rar
)

Save the index [4] value and you're done [trollface]
 
replace
Code:
$regex = '#(http|https):\/\/rapidshare\.com\/([^\s]+)#is';
with
Code:
$regex = '#(http|https):\/\/rapidshare\.com\/files\/(\d+)\/([^\s]+)#is';
 
Explode?

PHP:
<?php  

print_r(explode('/','https://www.rapidshare.com/files/265645559/some.file.rar'));  

?>


Result:
Code:
Array (     
[0] => https:     
[1] =>      
[2] => www.rapidshare.com     
[3] => files     
[4] => 265645559     
[5] => some.file.rar
)

Save the index [4] value and you're done [trollface]

Thanks, it's what I need :)
 
Wouldn't go for the explode method..
Stick with regex for such tasks.

If you paste a NL link between the RS links, the script will still handle the NL link.
But if you use the regex method, it'll simply ignore the NL link.
 
Status
Not open for further replies.

About the author

S
Active Member · Joined
741
Messages
59
Reactions
28
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom