webent Posted August 22, 2008 Share Posted August 22, 2008 Hi, I have an image path field in a csv file which includes both the image path and the image name... what I need to do is separate the two into two different strings to insert into the database... for instance, "http://images.supplier.com/products/474/794043110894.jpg" The thing is, the path is always going to be different, so I can't say, use "http://images.supplier.com/products/474/" as the path and whatever is behind it, use as the image name... Somehow I have to be able to tell it that everything after the LAST forward slash is the image name and what's left is the image path... Can anyone give me a hand with this? Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/ Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Use basename('http://images.supplier.com/products/474/794043110894.jpg') to retrieve the filename. Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-622935 Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 You can use both these methods: <?php $path = pathinfo('http://images.supplier.com/products/474/794043110894.jpg'); echo $dir = $path['dirname']; echo $file = $path['basename']; ?> or <?php $path = 'http://images.supplier.com/products/474/794043110894.jpg'; echo $file = substr($path, strrpos($path, '/') + 1); echo $dir = substr($path, 0, strrpos($path, '/')); ?> Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-622937 Share on other sites More sharing options...
webent Posted August 22, 2008 Author Share Posted August 22, 2008 Yes, I believe I'll go with this method, it seems like the simplest to accomplish both parts... <?php $path = pathinfo('http://images.supplier.com/products/474/794043110894.jpg'); echo $dir = $path['dirname']; echo $file = $path['basename']; ?> Thank you both for your help, I appreciate it. Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-622946 Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Your choice, but it's faster and simpler to use basename(). Example: <?php $path = 'http://images.supplier.com/products/474/794043110894.jpg'; $filename = basename($path); ?> Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-622965 Share on other sites More sharing options...
webent Posted August 22, 2008 Author Share Posted August 22, 2008 Yes, but that only gives me the image name, not the image path, I would still then have to do another function, subtracting the image name value, to get the image path... Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-622969 Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 Your choice, but it's faster and simpler to use basename(). I doubt it's faster then pathinfo(), as it has to do run the same string manipulation functions, but to get only the base name. While as for simplicity, in both cases you run only one function, but pathinfo() provides also the dirname. Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-622994 Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Yes, but that only gives me the image name, not the image path, I would still then have to do another function, subtracting the image name value, to get the image path... $path does contains the image path, "http://images.supplier.com/products/474/" would be the directory of the image in question. But yeah, I see what you mean. Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-623010 Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Your choice, but it's faster and simpler to use basename(). I doubt it's faster then pathinfo(), as it has to do run the same string manipulation functions, but to get only the base name. While as for simplicity, in both cases you run only one function, but pathinfo() provides also the dirname. I see, didn't get he needed the image directory. Link to comment https://forums.phpfreaks.com/topic/120848-solved-splitting-strings-2/#findComment-623011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.