Get Characters After Last/In Url

Get characters after last / in url

Very simply:

$id = substr($url, strrpos($url, '/') + 1);

strrpos gets the position of the last occurrence of the slash; substr returns everything after that position.


As mentioned by redanimalwar if there is no slash this doesn't work correctly since strrpos returns false. Here's a more robust version:

$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);

Get all characters after the last '/' (slash) from url string

For Swift 3 try this.

let fileName = "your/file/name/here"
let fileArray = fileName?.components(separatedBy: "/")
let finalFileName = fileArray?.last

Output : "Here"

How to get everything after last slash in a URL?

You don't need fancy things, just see the string methods in the standard library and you can easily split your url between 'filename' part and the rest:

url.rsplit('/', 1)

So you can get the part you're interested in simply with:

url.rsplit('/', 1)[-1]

Get the value of a URL after the last slash

As easy as:

substr(strrchr(rtrim($url, '/'), '/'), 1)

Replace everything after last slash in URL

Try this:

Search for: (.*/)(.*)$

Replace with:: $1authcode123$2

Explanation:

 (.*)       -  Anything inside two parens "()" is called a capture
group. The text that matches the pattern inside the
first set of parens is stored in "capture group 1".
You can use $1 to refer to this capture group when
you do your replace. Likewise $2 will refer to the
second capture group.

. - A period represents ANY CHARACTER.

.* - The asterisk "*" means "zero of more of the precedding
pattern." So .* would mean "zero or more occurrences
of ANY CHARACTER."

(.*/) - zero or more characters followed by a slash
(the results of this capture are stored in $1)

(.*)$ - zero or more chars followed by the end of the string
(the results of this capture are stored in $2)

Screenshot

However, you haven't shared all of the details of your requirements, so I don't know if this answer is complete.

For example, what would you do for a url that has no slashes?

Regex to catch everything after last '/' in URL

Use components(separatedBy:) and get the last component from it, i.e.

url1.components(separatedBy: "/").last //"exmaple1.jpg"
url2.components(separatedBy: "/").last //"11974343961963318467fl.png"
url3.components(separatedBy: "/").last //"01+Seth+Easy+v1.gif"

Or you can use lastPathComponent on the URL instance created using url1, url2 and url3

URL(string: url1)?.lastPathComponent //"exmaple1.jpg"
URL(string: url2)?.lastPathComponent //"11974343961963318467fl.png"
URL(string: url3)?.lastPathComponent //"01+Seth+Easy+v1.gif"

How to get string before last slash and after second last slash in URL

$str = explode("/","http://wwww.example/test1/test2/test3/");
echo $str[count($str)-2];

DEMO: https://eval.in/83914

get value after last slash (/)

You can use lastIndexOf and substring:

var uri= $('#item').css('background-image');
var lastslashindex = uri.lastIndexOf('/');
var result= uri.substring(lastslashindex + 1).replace(".png","");

or using .split() and .pop()

var uri= $('#item').css('background-image');
var result=uri.split("/").pop().replace(".png","");


Related Topics



Leave a reply



Submit