phorcon3 Posted May 23, 2008 Share Posted May 23, 2008 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 More sharing options...
kev wood Posted May 23, 2008 Share Posted May 23, 2008 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 More sharing options...
phpzone Posted May 23, 2008 Share Posted May 23, 2008 <?php $url = parse_url("http://www.domain.com/folder/ext_1.jpg"); $matches = array(); preg_match('/\/(.*)\/\w+_(.*)\.jpg/i', $url['path'], $matches ); var_dump( $matches ); ?> Link to comment https://forums.phpfreaks.com/topic/106938-parse_urlexplode/#findComment-548117 Share on other sites More sharing options...
micmania1 Posted May 23, 2008 Share Posted May 23, 2008 <?php $url = 'http://www.domain.com/folder/ext_1.jpg'; $dir = dirname($url); // folder $filename = basename($url); // ext_1.jpg $filename = expode('_', $filename); $fimage_id = substr($filename[1], 0, 1); echo $dir.'<br />'.$image_id; ?> Link to comment https://forums.phpfreaks.com/topic/106938-parse_urlexplode/#findComment-548121 Share on other sites More sharing options...
MatthewJ Posted May 23, 2008 Share Posted May 23, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.