0

How do I add a string after the last slash in url?

current url: https://example.org/gallery/images/my-image.jpg

I need to add the "thumbs/" character to the last slash

The function or php code must change the address as follows

https://example.org/gallery/images/thumbs/my-image.jpg

please guide me

2 Answers 2

1

There are a lot of ways. Try using URL and path functions and replacing:

$string = str_replace($dir=dirname(parse_url($string, PHP_URL_PATH)),
                      "$dir/thumbs",
                      $string);

Or string functions:

$string = str_replace($s=strrchr($string, '/'), "/thumbs$s", $string);
Sign up to request clarification or add additional context in comments.

Comments

0

Concatenate the strings.

<?php
     $addString = "thumbs/";
     $newURL = "https://example.org/gallery/images/" . $addString . "my-image.jpg";

     echo $newURL;

3 Comments

Thankful. But the URLs are dynamic and they come from another source so I just need to add string after last slash
What is that source? You can just change the value of $addString to the value returned from the source.
I receive addresses from a remote server. Addresses are images. There is a thumbs folder on the same server and path. I need to add this string to the last slash to get the thumbnails!.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.