开发者

Regex Between 2 /

开发者 https://www.devze.com 2023-03-23 00:01 出处:网络
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 differen

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号