i have a stri开发者_开发问答ng that can be either /mens/plain/
or /mens/plain
I wish to get just the plain
from the string, how would i do this with regex. Bare in mind mens and plain will be different each time. My regex system will be php
Thanks
There is no need for regex - use the basename function instead. To illustrate:
echo basename('/mens/plain/');
echo basename('/mens/plain');
$str = "/mens/plain/";
if (preg_match('|([^/]+)/?$|', $str, $matches))
echo $matches[1];
You can use explode()
$segments = array_filter(explode('/', $path));
array_filter()
will remove every empty string. After that, you can access the values directly through their "position" as index of the array
echo $segments[1]; // 'plain'
精彩评论