Page 1 of 1

Trimming and Replacing Text to Create New String

Posted: Sun Oct 23, 2005 10:13 pm
by BZorch
I have tried to use the following unsuccessfully:

trim()
preg_replace()
strstr()

I need to take a file location in my database similar to the following:

C:\mysite\Europe\Spain\Barcelona\Misc\Barcelona.jpg

and convert it to the following:

Thumbnails/mysite/Europe/Spain/Barcelona/Misc/Barcelona.jpg

It seems I would use trim() to trim the C:\ and then preg_replace() to convert the backslashes to forward slashes

Then add the Thumbnails string using .

Am I in the ballpark? I know it is very simple, but I just can not get it right.

Any insight would be appreciated

Posted: Sun Oct 23, 2005 10:35 pm
by hanji
Hello

This is quick and dirty... curious to see other ideas..

Code: Select all

<?
$val            = "C:\mysite\Europe\Spain\Barcelona\Misc\Barcelona.jpg";
$val            = str_replace("\\","/",$val);
$val            = "Thumbnails".str_replace("C:","",$val);
echo $val;
?>
HTH
hanji

Re: Trimming and Replacing Text to Create New String

Posted: Sun Oct 23, 2005 10:35 pm
by feyd
BZorch wrote:Am I in the ballpark?
Not especially in the area.. depending on flexibility/intelligence in the conversion, you can use substr() to remove the C:, standard string concatenation to add 'thumbnails', and str_replace() to change the '\' to '/'

Posted: Mon Oct 24, 2005 7:47 pm
by BZorch
Thank you for the responses. They worked flawlessly.