tags:

views:

69

answers:

4

PHP beginner's question.

I need to keep image paths as following in the database for the admin backend.

../../../../assets/images/subfolder/myimage.jpg

However I need image paths as follows for the front-end.

assets/images/subfolder/myimage.jpg

What is the best way to change this by PHP?

I thought about substr(), but I am wondering if there is better ways.

Thanks in advance.

A: 

you should save your image path in an application variable and can access from both admin and frontend

Adeel
A: 

If ../../../../ is fixed, then substr will work. If not, try something like this:

newpath=substr(strpos(path, "assets"));
petersohn
A: 

It might seem like an odd choice at first but you could use ltrim. In the following example, all ../'s will be removed from the beginning of $path. The dots in the second argument have to be escaped because PHP would treat them as a range otherwise.

$path = ltrim('../../../../assets/images/subfolder/myimage.jpg', '\\.\\./');

$path will then be:

assets/images/subfolder/myimage.jpg
lamas
Note that the second parameter of `ltrim` denotes a *list* of characters that ought to be removed.
Gumbo
A: 

I suggest this

$path = "../../../../assets/images/subfolder/myimage.jpg";
$root = "../../../../";

$root_len = strlen($root);

if(substr($path, 0, $root_len) == $root){
    echo substr($path, $root_len);
} else {
    //not comparable
}

In this way you have a sort of control on which directory to consider as root for your images

Nicolò Martini