Materialized Path PHP Regex to Select Last Item
I'm trying to get the last occurrence of an item from a materialized path. Below is a subset of possible values I've provided as an example where X, Y & Z can be any arbitrary string: X/ X/Y/ X/Y/Z/ How can I select that last item on the path using php regex which would output the following for the corresponding lines above: X Y Z
$parts = explode('/', trim($url, '/')); $lastpart = end($parts); No need for regex. But if you insist: #^/?([^/]+)/?$# Path part is in group 1.
Rather than a regex, use strrpos() to find the last / after trimming off the trailing /: $string = "/x/y/"; $string = rtrim($string, "/"); echo substr($string, strrpos($string, "/") + 1); // y $string = "/x/y/z/"; // prints // z $string = "/x"; // prints // x
I'd go for a preg_split and array_pop: $test = "a/a/v/"; $test = rtrim($test,'/'); $arr = preg_split('/\//',$test); $lastelement = array_pop($arr); var_dump($lastelement);
PHP issue calling static class from another static class
Error while sending HTTP request
PHP seems to add in backslashes when strings are concatenated
Querying Elasticsearch with PHP
MemcacheD multiple data saves per second
Button Form POST Not Working
Parse text file line by line and store data in MYSQL database
ERROR invalid-api-key PHP /CURL for Alchemy api text/TextGetCombinedData
Ionic/AngularJS 2nd request not sending data but getting success answer
Swift method to send email through MVC PHP page is very slow
Laravel 5.2 with PostgreSQL autoincrement not working, id must be filled
How to fix Travis-CI [Composer\Exception\NoSslException] with PHP 5.3.3?
PHP Download the json file
NotFoundHttpException in Handler.php while trying to update
Why won't my substr call work with the → character?
My alert doesn't show up in laravel 5