Get Last Word from Url After a Slash in PHP

Get last word from URL after a slash in PHP

by using regex:

preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test

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 the value of a URL after the last slash

As easy as:

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

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

How to get string after second slash in url - using php?

$last = explode("/", $str, 3);
echo $last[2];

Get last word from URL after equals sign

Considering you already have the URL in a variable, let's say

$url = 'http://examplesite.com/?skill-type=new;

One possible way (certainly there are others) would be:

$urlArray = explode('=',$url);
$last = $urlArray[sizeof($urlArray)-1];

Note:
only applicable if you don't know how the URL comes, if you do, consider on using $_GET

Get last word in URL and delete what is not necessary

$last_word = str_replace(array('-0.html','-0.htm'), '', $last_word);

Hope this helps. It will replace the string (-0.html or -0.htm) even if found somewhere else than at the end of $last_word.

how to get url last part after slash as get value in php

RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?slug=$1

but error is http://localhost/event_manage/hello

The above rule assumes your URL ends in a trailing slash, but your example does not, so it will fail to match and result in a 404 (because it's not rewriting the request to index.php).

It should be:

RewriteRule ^([\w-]+)$ index.php?slug=$1 [L]

\w (word characters) is just a shorthand character class, the same as [a-zA-Z0-9_].

You need the L flag if you add any more directives.

This assumes .htaccess and index.php are located inside the /event_manage subdirectory.


Aside:

http://localhost/event_manage?slug=hello

Since /event_manage is a physical directory, this URL should be http://localhost/event_manage/?slug=hello (with a trailing slash after the directory name). If you omit the trailing slash then mod_dir will append the trailing slash with a 301 redirect.

Get two words from URL after a slash in PHP

$string = 'http://mydomain.com/alrajhi/invoice/108678645541';
$parse = parse_url($string);
$explode = explode('/', $parse['path']);

Now you have:

echo $explode[1]; // alrajhi
echo $explode[3]; // 108678645541


Related Topics



Leave a reply



Submit