tags:

views:

49

answers:

5
string '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg' (length=85)

what i need is just http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg

what is the best way doing it ? i mean useing strlen ? substr_replace ? substr ? im a bit confused what is the best way doing this? becouse there is many ways to do this.

edit* there is no newbie tag :|

    // get from database red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
    $image_path = $this->data['products'][0]['image_small'];
    $exploded = end(explode('/', $image_path));
    $myurl = DOMAIN;
    $myfullurl = $myurl."/storage/".$exploded;

// it works!, but let see the comments maybe there is a better way :)

A: 

I suggest you check if the string contains /home/adam/Projects/red, and if it does, you use substr to get the part after it, and you glue it with http://localost.

$path = '/home/adam/Projects/red/storage/*snip*.jpg';
$basePath = "/home/adam/Projects/red";
if (strpos($path, $path) !== false)
    $url = 'http://localhost' . substr($path, strlen($basePath));
zneak
So a hard way..
Alexander.Plutov
@garvey: excluding the variables declaration, it's two lines.
zneak
+1  A: 

Here is how you can get the image part:

$str = '/home/adam/Projects/red/storag/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$exploded = end(explode('/', $str));
echo $exploded;

Result:

22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg

Now you can concatenate it with whatever eg:

$new_str = 'http://localhost/storage/' . $exploded;
echo $new_str;

Result:

http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg

And It is most likely you want to concatenate the image path with your document root which you do like this:

$img_path = $_SERVER['DOCUMENT_ROOT'] . $exploded;

The idea is that you explode the string with explode function by specifying / as delimiter. This gives you array, now you use the end function to get the ending part of the array which is your image actually.

Sarfraz
wew, ive neverthink about that! . let me try :)
Adam Ramadhan
A: 

This one's pretty much the easiest

str_replace(
  "/home/adam/Projects/red", 
  "http://localhost", 
  "/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg"
);
Maerlyn
Though it's unlikely, you'll break the string if `/home/adam/Projects/red` is present multiple times inside the path.
zneak
@zneak Indeed, but that would be extremely unlikely.
Maerlyn
A: 
$string = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
str_replace('/home/adam/Projects/red', 'http://localost', $string)
Alexander.Plutov
It's unlikely, but you'll get erroneous results if `$string` contains `/home/adam/Projects/red` multiple times.
zneak
+1  A: 

If the path prefix represents your document root path, then you can do this to strip it:

$path = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$_SERVER['DOCUMENT_ROOT'] = '/home/adam/Projects/red/';
if (substr($path, 0, strlen($_SERVER['DOCUMENT_ROOT'])) === $_SERVER['DOCUMENT_ROOT']) {
    $uriPath = substr($path, strlen(rtrim($_SERVER['DOCUMENT_ROOT'], '/')));
    echo $uriPath;
}
Gumbo