Adam Ramadhan
Adam Ramadhan

Reputation: 22810

best way to replace a string?

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 :)

Upvotes: 0

Views: 240

Answers (5)

Sarfraz
Sarfraz

Reputation: 382696

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.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655239

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;
}

Upvotes: 1

Alex Pliutau
Alex Pliutau

Reputation: 21957

$string = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
str_replace('/home/adam/Projects/red', 'http://localost', $string)

Upvotes: 0

Maerlyn
Maerlyn

Reputation: 34107

This one's pretty much the easiest

str_replace(
  "/home/adam/Projects/red", 
  "http://localhost", 
  "/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg"
);

Upvotes: 0

zneak
zneak

Reputation: 138051

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));

Upvotes: 0

Related Questions