tail Posted June 10, 2009 Share Posted June 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 10, 2009 Share Posted June 10, 2009 $_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 Quote Link to comment Share on other sites More sharing options...
youngmonkey Posted June 10, 2009 Share Posted June 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
tail Posted June 10, 2009 Author Share Posted June 10, 2009 $_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? Quote Link to comment Share on other sites More sharing options...
youngmonkey Posted June 10, 2009 Share Posted June 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
tail Posted June 11, 2009 Author Share Posted June 11, 2009 Yes, this is correct. However, as mentioned in the first post, the URL changes sometimes on varying games. Will this script do as said with different numbers of "/"? Quote Link to comment Share on other sites More sharing options...
youngmonkey Posted June 11, 2009 Share Posted June 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
tail Posted June 11, 2009 Author Share Posted June 11, 2009 OK I don't think the folder names vary, but I realized the IP's do. However, all the IP's resolve to "www.kontraband.com". Is there a way to replace the IP address with the website? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 11, 2009 Share Posted June 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 11, 2009 Share Posted June 11, 2009 parse_url Quote Link to comment Share on other sites More sharing options...
youngmonkey Posted June 11, 2009 Share Posted June 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 11, 2009 Share Posted June 11, 2009 $swf = 'http://208.116.9.205/1/graphics/games/11623/games_BillySuicide.swf'; $thumb = preg_replace('#[^/]+\.swf$#', 't.jpg', $swf); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.