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);
Full-Text Search Function not working at all
WordPress settings page for custom methods
laravel get jobs from redis queue located in another codebase\project?
How to concatenate variable and string that contains () in php
Laravel mysql multiple operations with one DB instance
How to add form data to a bootstrap modal in the same page by using jQuery?
WP - How to get all the comment_post_ID in bulk comment approval
How to grab a random MYSQL Column Value using PHP and echo it?
Secure way to allow a specific PHP script to read/write a directory?
php searching empty fields in the db [duplicate]
PHP strtotime returns incorrect month [duplicate]
Listing out team members with select options
Restart foreach loop in PHP
Can't map request body json into injected response object
Can't access local php website (Linux)
php code not work, need some change [duplicate]