Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract last part of a url

Tags:

php

need the last part of the url

http://example.com then the last part is nothing if it is http://example.com/i then last part is i if it is http://example.com/i/am/file.php then last part is file.php

not sure if I use regex or what

like image 201
Asim Zaidi Avatar asked May 12 '11 20:05

Asim Zaidi


People also ask

How do I pull the last part of a URL?

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substring() function to return the substring starting from that location: console. log(this. href.

How can I get the last part of a URL in PHP?

Get Last URL Segment If you want to get last URI segment, use array_pop() function in PHP.

How do I get the current URL?

The current URL can be obtained by using the 'URL' property of the Document object which contains information about the current URL. The 'URL' property returns a string with the full location of the current page, along with containing the string having the HTTP protocol such as ( http://).


2 Answers

This is a simple example:

 <?php
     $url = "http://example.com/i/am/file.php";
     $keys = parse_url($url); // parse the url
     $path = explode("/", $keys['path']); // splitting the path
     $last = end($path); // get the value of the last element 
 ?>

I hope this help you ;)

like image 136
Vasil Dakov Avatar answered Oct 18 '22 08:10

Vasil Dakov


There is a function called parse_url().

like image 30
Denis de Bernardy Avatar answered Oct 18 '22 06:10

Denis de Bernardy