Jump to content

[SOLVED] splitting strings 2


webent

Recommended Posts

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

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, '/'));
?>

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.

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.

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.

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.

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.