Vaios P.
Vaios P.

Reputation: 404

How to change a part of a string from a database when echoing it

I have this:

<a href="">
    <img class="img" src="
        <?php $query = mysql_query("
            SELECT * FROM posts WHERE ID = 49");
            while($row = mysql_fetch_array( $query )) 
            { echo $row['Image1(170x170)']; } 
    ?>" width="180px" height="130px">
</a>

I want to echo an image path where I have the echo that I have stored the path into a row in a database... In generally I have stored the image path like this:
../folder/folder/file.jpg for another reason that I cant changed it and now I want to pull that from the database and echo it here but I want to change and done like this from
../folder/folder/file.jpg to folder/folder/folder/file.jpg
Is there a way to do that?

Because I have searched for a lot of time and I have only find the REPLACE() that I don't want to use because I don't want to change my records in my database. Any help would be appreciated! Thanks in advance!

Upvotes: 1

Views: 96

Answers (2)

Amal Murali
Amal Murali

Reputation: 76656

I want to change and done like this from ../folder/folder/file.jpg to folder/folder/folder/file.jpg

The basic idea here is to get your image src link, and trim the first two .. characters. This can be done in many ways. I've just str_replace() in the below code:

$query = mysql_query("SELECT * FROM posts WHERE ID = 49");
while($row = mysql_fetch_array( $query )) 
{ 
    $src = $row['Image1(170x170)']; 
    $src = str_replace('..', '', $src);
    ?>
    <a href=""><img class="img" src="<?php echo $src; ?>" width="180px" height="130px"/>
    <?php
} 

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53535

You can do it in the PHP code:
Remove the .. from the beginning of the string using substr and add the "folder" instead:

echo "folder" . substr($row['Image1(170x170)'],2);

Upvotes: 1

Related Questions