Parsing the last substring of a URL
I want to parse the line after the last "/". For example:
http://127.0.0.1/~dtm/index.php/en/parts/engine
Disassemble the "engine".
I tried to do it with Regexp, but as new to regexp that was next to the solution. Also this pattern seems to be quite easily destructible (/ engine / will break it). We need to make it a little more stable somehow.
$pattern = ' \/(.+^[^\/]?) ' ;
- / Match / char
- ... + Matches any char, one or more
- ^ [^ / \ Exclude \ char
+3
source to share
4 answers
You can also use parse_url () , explode () and array_pop () to achieve your goal.
<?php
$url = 'http://127.0.0.1/~dtm/index.php/en/parts/engine';
$parsed = parse_url($url);
$path = $parsed['path'];
echo array_pop(explode('/', $path));
?>
PhpFiddle Demo
+1
source to share