Jump to content

parse_url/explode?


phorcon3

Recommended Posts

i wanna parse this exact link:

 

http://www.domain.com/folder/ext_1.jpg

 

and what i need is:

 

http://www.domain.com/folder/ext_1.jpg

 

what i got so far is:

 

<?php

$url=parse_url('http://www.domain.com/folder/ext_1.jpg');

$t=explode('/',$url['path']);

echo $t[1];

?>

 

that would give back folder

 

and then use explode again ..and explode ...but thats just ..i dunno dumb lol ..wonder if theres a better, cleaner way to do this?

 

Link to comment
https://forums.phpfreaks.com/topic/106938-parse_urlexplode/
Share on other sites

this code will parse url and give you the path

 

<?php

  $p = parse_url ( 'http://domain.tld/tcp://domain2.tld/dir/file' ) ;

  $d2 = parse_url ( $p['path'] ) ;

  echo $d2 ;  // returns '/dir/file'

?>

 

for a better explanation of the code check this website out  http://us.php.net/function.parse-url

Link to comment
https://forums.phpfreaks.com/topic/106938-parse_urlexplode/#findComment-548112
Share on other sites

Here is my take...

 

<?php

$url = 'http://www.domain.com/folder/ext_1.jpg';

$dir =  dirname($url); // folder
$filename = basename($url); // ext_1.jpg
$foldername = explode("/", $dir);
$filename = explode('_', $filename);
$stopper = strpos($filename[1], ".");
$fimage_id = substr($filename[1], 0, $stopper);

echo "Directory is: $foldername[3]<br />";
echo "Image id is: $fimage_id<br />";

?>

 

That will also allow for image numbers with more than one digit.

Link to comment
https://forums.phpfreaks.com/topic/106938-parse_urlexplode/#findComment-548238
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.