admin管理员组文章数量:1405558
I have the following url. .png
Everything in the url can change except the userfiles part and the last underscore. Basically I want to get the part of the url which is userfiles/dynamic/images/whatever_dollar_
What is a good way to do this. I'm open or both JavaScript or php.
I have the following url. http://domain./userfiles/dynamic/images/whatever_dollar_1318105152.png
Everything in the url can change except the userfiles part and the last underscore. Basically I want to get the part of the url which is userfiles/dynamic/images/whatever_dollar_
What is a good way to do this. I'm open or both JavaScript or php.
- php/manual/en/function.parse-url.php – dm03514 Commented Oct 8, 2011 at 20:44
3 Answers
Reset to default 5Use parse_url in PHP to split an url in its various parts. Get the path
part that is returned. It contains the path without the domain and the query string.
After that use strrpos to find the last occurrance of the _
within the path.
With substr you can copy the first part of the path (up until the found _
) and you're done.
You could, with JavaScript, try:
var string = "http://domain./userfiles/dynamic/images/whatever_dollar_1318105152.png";
var newString = string.substring(string.indexOf('userfiles'),string.lastIndexOf('_'));
alert(newString); // returns: "userfiles/dynamic/images/whatever_dollar" (Without quotes).
JS Fiddle demo.
References:
substring()
.indexOf()
.lastIndexOf()
.
Assuming your string is stored in $s, simply:
echo preg_replace('/.*(userfiles.*_).*/', '$1', $s);
本文标签: phpget a specific part of a stringStack Overflow
版权声明:本文标题:php - get a specific part of a string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744334129a2601106.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论