Replace part of the string

亡梦爱人 提交于 2019-12-12 02:45:17

问题


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/


回答1:


$url = 'http://342.234.243.142/data/somethingmore/';
echo basename($url);



回答2:


$str = "http://342.234.243.142/data/somethingmore/";
$str = explode('/', $str);

print_r($str);



回答3:


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/



来源:https://stackoverflow.com/questions/6662777/replace-part-of-the-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!