...

View Full Version : Extracting the end of a URL



Anthony2oo4
02-18-2006, 03:49 PM
Hey there, I am trying too make a script that can check URL's, which I managed too do with no problem at all. I was wondering was there a way my users can enter multiple urls into a textbox and have the script extract the end of each URL.

For example, the user submits:

http://mydomain.com/files/13039621/2k5pro.part28.rar.html

I want the script too delete the first part so it is left with:

/13039621/2k5pro.part28.rar.html

and place it in an array. It would also need to be able to take a bunch of links at one time, such as:

http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/13242343/2k5pro.part29.rar.html
http://mydomain.com/files/13423621/2k5pro.part30.rar.html
http://mydomain.com/files/13044421/2k5pro.part31.rar.html
http://mydomain.com/files/13252331/2k5pro.part32.rar.html
http://mydomain.com/files/12449621/2k5pro.part33.rar.html

Thanks for your time and effor in helping me.:thumbsup:

fci
02-18-2006, 03:57 PM
here you go:
http://us3.php.net/parse_url


$url_parts = parse_url($url);
echo $url_parts['path'];

fci
02-18-2006, 04:06 PM
ah, I had only answered part of your question.. here's the rest of your answer...

<?php
$urls = <<<URLS
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
URLS;

$urls = split("\n", $urls);

$paths = array();
foreach ($urls as $url) {
$path = parse_url($url);
$paths[] = $path['path'];
}

print_r($paths);
?>

Anthony2oo4
02-18-2006, 05:37 PM
Hey, thanks for the quick reply,

I suppose I should have mentioned that im sort of new too PHP lol, so go easy on me :)

I tried the code you wrote in your last post and here's wha tit outputted:

Array ( [0] => /files/35539621/2k5pro.part28.rar.html_ [1] => /files/35539621/2k5pro.part28.rar.html_ [2] => /files/35539621/2k5pro.part28.rar.html_ [3] => /files/35539621/2k5pro.part28.rar.html_ [4] => /files/35539621/2k5pro.part28.rar.html_ [5] => /files/35539621/2k5pro.part28.rar.html )

I see that you got it too strip it down and you learned me a new function, with this I tried too make my own code to have a go because your code confused me:


<?

$url = "
http://www.whereisit.org/whereis/1.rar.html
http://www.whereisit.org/whereis/2.rar.html
http://www.whereisit.org/whereis/3.rar.html
http://www.whereisit.org/whereis/4.rar.html
http://www.whereisit.org/whereis/5.rar.html
http://www.whereisit.org/whereis/6.rar.html
";

$urls = split("\n", $urls);

$num = count($urls);
$i = 0;
while ($i < $num) {
$url = parse_url($url);
$url = $url['path'];
echo "$url";
$i++;
}
?>

but this outputted:

/whereis/1.rar.html__http://www.whereisit.org/whereis/2.rar.html__http://www.whereisit.org/whereis/3.rar.html__http://www.whereisit.org/whereis/4.rar.html__http://www.whereisit.org/whereis/5.rar.html__http://www.whereisit.org/whereis/6.rar.html__

lol, which is not what I was going for; so back to your code. is that what you wanted it too output?

Is there anyway of just getting it too output

/files/35539621/2k5pro.part28.rar.html
/files/35539621/2k5pro.part28.rar.html
/files/35539621/2k5pro.part28.rar.html
/files/35539621/2k5pro.part28.rar.html
/files/35539621/2k5pro.part28.rar.html

?

Thanks for your reply and your time :)

fci
02-18-2006, 06:23 PM
<?php
$urls = <<<URLS
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
http://mydomain.com/files/35539621/2k5pro.part28.rar.html
URLS;

$urls = split("\r\n", $urls);

$paths = array();
foreach ($urls as $url) {
$path = parse_url($url);
$path['path'].'<br />';
}

?>
like that?

Anthony2oo4
02-18-2006, 07:25 PM
hmm, I may have got it wrong. I user your original code and printed the results like this:

echo"$paths[1] $paths[2]";

But now it prints them with a "_" at the end.

/files/35539621/2k5pro.part28.rar.html_
/files/35539621/2k5pro.part28.rar.html_

How ca I get rid of the _ at the end ?

Thanks again.

goughy000
02-18-2006, 07:31 PM
How ca I get rid of the _ at the end ?



echo rtrim("$paths[0]", "_");

Anthony2oo4
02-18-2006, 07:39 PM
Thanks both for your contributes, I think I can manage from here, but I will reply if I need any mre help. Once again, thanks ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum