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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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