Solved

Add text to the database image path

Posted on 2013-11-18
5
272 Views
Last Modified: 2014-03-16
I need to display images on my site that take image path values like this from the database...

uploads/album_17/55487_0f635b_a0c242.jpg

I need to change these path to..

uploads/album_17/th_155487_0f635b_a0c242.jpg

Any idea how (using PHP) I can add that 'th_1' bit to the path?
0
Comment
Question by:BrighteyesDesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39657907
Please see http://www.laprbass.com/RAY_temp_brighteyesdesign.php

As with any data-dependent problem the quality of the answers you get will be directly related to the quality of the test data you give us.

<?php // RAY_temp_brighteyesdesign.php
error_reporting(E_ALL);

/** SEE: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28297539.html
 * Source: uploads/album_17/55487_0f635b_a0c242.jpg
 * Result: uploads/album_17/th_155487_0f635b_a0c242.jpg
 */

// THE DATA EXAMPLES
$source = 'uploads/album_17/55487_0f635b_a0c242.jpg';
$result = 'uploads/album_17/th_155487_0f635b_a0c242.jpg';

// TRANSFORM THE SOURCE INTO THE RESULT
$newtxt = str_replace('17/', '17/th_1', $source);

// TEST THE TRANSFORMATION
if ($newtxt == $result) echo "SUCCESS!";

Open in new window

Cheers, ~Ray
0
 

Author Comment

by:BrighteyesDesign
ID: 39667448
Thanks Ray,

Apologies but I should have explained that the album number changes too. So when the next gallery is uploaded it'll be album_18

I've attached a screenshot of the database which might make things clearer.

Is it possible (using php) to delete everything up to and including the second '/' and replacing it with something along the lines of...

uploads/album_$AlbumID/th_155487_0f635b_a0c242.jpg

Hope this makes sense!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39667557
attached a screenshot of the database...

?

0
 

Author Comment

by:BrighteyesDesign
ID: 39668362
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39668686
Maybe something like this?  It's all about string concatenation -- the process of assembling a character string (in this case the album image path) from component parts.  Concatenation is done with the dot (period) in PHP.  See also this note:
http://us3.php.net/manual/en/ref.strings.php#51100

<?php // RAY_temp_brighteyesdesign.php
error_reporting(E_ALL);

/** SEE: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28297539.html
 * Source: uploads/album_17/55487_0f635b_a0c242.jpg
 * Result: uploads/album_17/th_155487_0f635b_a0c242.jpg
 */

// THE DATA EXAMPLES
$albumID = '17';
$source  = 'uploads/album_XX/55487_0f635b_a0c242.jpg';
$result  = 'uploads/album_17/th_155487_0f635b_a0c242.jpg';

// TRANSFORM THE SOURCE INTO THE RESULT
$substrs = explode('/', $source);
$newtxt  = current($substrs) . DIRECTORY_SEPARATOR . "album_$albumID" . DIRECTORY_SEPARATOR . 'th_1' . end($substrs);

// TEST THE TRANSFORMATION
if ($newtxt == $result) echo "SUCCESS!";

Open in new window

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question