Get Last Part of URL PHP
You can use preg_match
to match the part of the URL that you want.
In this case, since the pattern is easy, we're looking for a forward slash (\/
and we have to escape it since the forward slash denotes the beginning and end of the regular expression pattern), along with one or more digits (\d+
) at the very end of the string ($
). The parentheses around the \d+
are used for capturing the piece that we want: namely the end. We then assign the ending that we want ($end
) to $matches[1]
(not $matches[0]
, since that is the same as $url
(ie the entire string)).
$url='http://domain.example/artist/song/music-videos/song-title/9393903';
if(preg_match("/\/(\d+)$/",$url,$matches))
{
$end=$matches[1];
}
else
{
//Your URL didn't match. This may or may not be a bad thing.
}
Note: You may or may not want to add some more sophistication to this regular expression. For example, if you know that your URL strings will always start with http://
then the regex can become /^http:\/\/.*\/(\d+)$/
(where .*
means zero or more characters (that aren't the newline character)).
URL - Get last part in PHP
use following
<?php
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/',$link);
echo $page = end($link_array);
?>
How to get URL last part with php
As stated in the comments, the best way is to explode()
then array_pop()
your URL.
Like so:
function curPageURL() {
$url = $_SERVER['REQUEST_URI'];
$url = explode('/', $url);
$lastPart = array_pop($url);
return $lastPart;
}
@Vineet answer is suitable too.
Extract Last Part of URL using PHP
this piece of code is work for me
$id = substr($url, strrpos( $url, '/' )+1);
strrpos
gets the position of the last occurrence of the slash; substr
returns everything after that position.
Get the last string of URL's
Use the explode
method to get array of URL
<?php
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/',$link);
echo $page = end($link_array);
?>
If your output is
page/page/pageIWant
page/page/pageIWant
<?php
$text = "page/page/pageIWant";
$text_arr = explode('/',$text);
echo $page = end($text_arr);
// this will print: pageIWant
?>
How to get last segment of the URL without GET variables in PHP?
You can use strtok
to exclude the query strings:
$url = strtok($_SERVER['REQUEST_URI'], '?');
$requred_string = substr(strrchr($url, '/'), 1);
echo $requred_string;
Or as mentioned in the comments, parse_url
will work also:
$requred_string = substr(strrchr(parse_url($url)['path'], '/'), 1); // PHP 5.4 or greater with dereference
pcre. How to get last part of url
The preg_replace
only replaces what you have it find. In this case product-123.html
. So you're replacing /product-123.html
with product-123.html
and the https://example.com.ua/part1/part2/part3
remains untouched.
To replace everything and only keep the match you'd do
echo
preg_replace('#.*/([a-zA-Z0-9_-]+\.html)$#','$1','https://example.com.ua/part1/part2/part3/product-123.html');
you don't need a regex though to accomplish this task, and if you did it'd probably be cleaner to use preg_match
.
Here's a preg_match
approach:
preg_match('#[a-zA-Z0-9_-]+\.html$#', 'https://example.com.ua/part1/part2/part3/product-123.html', $match);
echo $match[0];
Demo: https://3v4l.org/4o9RM
Regex demo: https://regex101.com/r/6dytu0/2/
extract last part of a url
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 ;)
How to get the last path in a URL?
Try:
$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
Assuming $tokens
has at least 2 elements.
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);
Related Topics
PHP MySQLi Prevent SQL Injection
Is There a PHP Library For Email Address Validation
Causes of MySQL Error 2014 Cannot Execute Queries While Other Unbuffered Queries Are Active
Posting Form Fields With Same Name Attribute
Simple Postgresql Statement - Column Name Does Not Exists
Can HTML Be Embedded Inside PHP "If" Statement
How to Remove an Array Element in a Foreach Loop
Send Array With Ajax to PHP Script
MySQLi_Query() Expects Parameter 1 to Be MySQLi, Object Given
How to Iterate Over the Results in a MySQLi Result Set
Fastest Way to Serve a File Using PHP
PHP Json_Encode Class Private Members
Posting Raw Image Data as Multipart/Form-Data in Curl
PHP: Storing 'Objects' Inside the $_Session
PHP Foreach Pass by Reference: Last Element Duplicating (Bug)
PHP Algorithm to Generate All Combinations of a Specific Size from a Single Set