Jump to content

Getting part of a URL


tail

Recommended Posts

I have a script scrapes a flash game site. The flash file URL comes up in a different format on each different page. In order to get the thumbnail image of the game, I have to replace a certain part of the filename with "t.jpg".

Example: Flash File - http://208.116.9.205/1/graphics/games/11623/games_BillySuicide.swf

Thumbnail - http://208.116.9.205/1/graphics/games/11623/t.jpg

 

Now that I have the flash URL, how can I get "http://208.116.9.205/1/graphics/games/" into a string? The folders come up different sometimes.

Link to comment
Share on other sites

$_SERVER['HTTP_HOST']

 

would return http://208.116.9.205/1/graphics/games/11623/ from the full URL http://208.116.9.205/1/graphics/games/11623/games_BillySuicide.swf

 

If you are always looking for the full path minus the actual script or file being parsed this is the variable you'd want to use.

 

http://us.php.net/manual/en/reserved.variables.server.php

Link to comment
Share on other sites

Hi,

Assuming you have the url already, the following code should probably help:

<?php
$url = "http://208.116.9.205/1/graphics/games/11623/games_BillySuicide.swf";
$url = substr($url,0,strrpos($url,"/"));
$url = substr($url,0,strrpos($url,"/"));
$url .= "/";
echo $url . "\n" ;
?>

 

This gives :  http://208.116.9.205/1/graphics/games/

 

What this snippet does is simply remove whatever is after the last "/". Since you need the part of url thats before the last two "/", I did a substr twice.

 

You might want to look at strtok if you need a more robust/scalable solution.

 

Thanks,

M.

Link to comment
Share on other sites

$_SERVER['HTTP_HOST']

 

would return http://208.116.9.205/1/graphics/games/11623/ from the full URL http://208.116.9.205/1/graphics/games/11623/games_BillySuicide.swf

 

If you are always looking for the full path minus the actual script or file being parsed this is the variable you'd want to use.

 

http://us.php.net/manual/en/reserved.variables.server.php

 

$_SERVER['HTTP_HOST'] wouldn't work when accessing a remote server, would it?

Link to comment
Share on other sites

Tail,

 

I understood the following from your first post:

 

1. You already have the flash file URL.

2. You need to generate the thumbnail URL by replacing a part of the URL with /t.jpg.

3. For 2, you need to remove the part of the URL after the second last "/" from the.

 

Are these assumptions correct? If they are, the snippet I posted should do the trick-The url being echoed in the snippet is the string that you said you needed in that first post, and it does not matter what the folders are.

If not, correct me where I am wrong.

 

Thanks,

M.

Link to comment
Share on other sites

Tail,

It will strip whatever's after the second last "/". So if only the folder names vary, yes, this will work. If the position of the slash after which you want to append the t.jpg changes, it would be quite impossible to figure that out.

If you had http://208.116.9.205/1/2/3/4/5.swf , it would give you

http://208.116.9.205/1/2/3/ as the string.

 

Thanks,

Santosh.

 

 

 

 

Link to comment
Share on other sites

I had a look at the site, and for most games, the thumbnail is simply found by swapping the swf filename with t.jpg in the path. But with some games (like http://208.116.9.205/1/graphics/games/just_pitching_golf.swf), the thumbnail is named something else than t.jpg (golf.jpg with that game), and it resides on another server (but that could be solved by using www.kontraband.com instead of the IP, I guess). So I can't see how we can get the thumbnail each time. But hey, that might be because Kontraband don't want us to.

Link to comment
Share on other sites

Tail,

This should help. If you want to change only the filename, set $temp[7] to 't.jpg' and leave $temp[6] alone.

<?php 
$url = "http://208.116.9.205/1/graphics/games/11623/games_BillySuicide.swf";
$temp = explode("/",$url);
$temp[2] = "www.kontraband.com";
unset ($temp[7]);
$temp[6] = "t.jpg";
$final_url = implode("/",$temp);
echo $final_url;
?>

 

The final_url it gives is :http://www.kontraband.com/1/graphics/games/t.jpg

Hope this helps,

M.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.