Replace part of the string

2019-08-30 08:42发布

Echo $var gives me something like http://342.234.243.142/data/somethingmore/

IP-address everytime is different. Instead of "somethingmore" can something like "/folder/images/2011/gallery/file.jpg" (anything)

How do I strip from this string part with the ip and /data/ folder?

$var could become /somethingmore/

3条回答
We Are One
2楼-- · 2019-08-30 08:42

Use this regular expression:

/.*?\/data/

This way you can get a full string after '/data' even if it contains more '/' (slashes). For example, http://342.234.243.142/data/somethingmore/andmore/

查看更多
混吃等死
3楼-- · 2019-08-30 08:46
$url = 'http://342.234.243.142/data/somethingmore/';
echo basename($url);
查看更多
乱世女痞
4楼-- · 2019-08-30 08:49
$str = "http://342.234.243.142/data/somethingmore/";
$str = explode('/', $str);

print_r($str);
查看更多
登录 后发表回答