Trimming and Replacing Text to Create New String

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BZorch
Forum Commoner
Posts: 45
Joined: Mon May 02, 2005 10:42 pm

Trimming and Replacing Text to Create New String

Post 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
User avatar
hanji
Forum Commoner
Posts: 46
Joined: Fri Apr 29, 2005 3:23 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Trimming and Replacing Text to Create New String

Post 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 '/'
BZorch
Forum Commoner
Posts: 45
Joined: Mon May 02, 2005 10:42 pm

Post by BZorch »

Thank you for the responses. They worked flawlessly.
Post Reply