t_machine Posted June 14, 2009 Share Posted June 14, 2009 Hi, I have links from a database that contains the image name and type. How can i strip the name and file type from the link. Example: $string = 'http://mysite.com/images/myimage.png'; How can I parse that string and get myimage.png Note also that the file type is not always .png, it is also .jpg, .jpeg, .gif.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/162170-how-to-get-image-file-name-from-string-in-php/ Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 basename explode Quote Link to comment https://forums.phpfreaks.com/topic/162170-how-to-get-image-file-name-from-string-in-php/#findComment-855794 Share on other sites More sharing options...
pkedpker Posted June 14, 2009 Share Posted June 14, 2009 simple job for substr $file_extension = substr($filename , strrpos($filename , '.') +1); would return jpg, gif won't keep the dot. strrchr($filename, '.') would return .jpg .gif but it will keep the dot. another function called basename $filename = "C:\test\to\etc\theimagefile.jpg"; basename($filename); would return "theimagefile.jpg" $file = basename($filename, ".jpg"); would return "theimagefile" Quote Link to comment https://forums.phpfreaks.com/topic/162170-how-to-get-image-file-name-from-string-in-php/#findComment-855797 Share on other sites More sharing options...
t_machine Posted June 14, 2009 Author Share Posted June 14, 2009 Thanks very much, works great Quote Link to comment https://forums.phpfreaks.com/topic/162170-how-to-get-image-file-name-from-string-in-php/#findComment-855823 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.