I have a path that look开发者_如何学JAVAs like:
/home/duke/aa/servers/**servername**/var/...morefiles...
With php, I want to extract the "servername" from the path
Unfortunately I'm not that well versed with php but I came up with something that used strstr()
but I am only using PHP version 5.2 where as one of the parameter functions require 5.3
What could be some code that would return "servername"?
you can use explode('/', $path)
to break it down into the individual directories. After that, it's up to you to figure out which array element is the server name (with your sample path, it'd be #4):
$parts = explode('/', $path);
echo $parts[4]; // **servername**
function getServerName($data) {
preg_match('#/servers/(.+)/var/#', $data, $result);
if (isset($result[1]) {
return $result[1];
}
}
$data = '/home/duke/aa/servers/**servername**/var/...morefiles...';
echo getServerName($data);
$str = '/home/duke/aa/servers/**servername**/var/...morefiles...';
echo preg_replace('$(.+)/servers/(.+)/var/(.+)$', '\2', $str);
精彩评论