拿这个域:
http://www.?.co.uk/elderly-care-advocacy/mental-capacity-act-advance-medical-directive.html
我如何使用PHP查找第一个和第二个斜杠之间的所有内容,无论它是否发生变化?
IE中。老年护理倡导
任何人都会非常感激。
答案 0 :(得分:5)
//strip the "http://" part. Note: Doesn't work for HTTPS!
$url = substr("http://www.example.com/elderly-care-advocacy/mental-capacity-act-advance-medical-directive.html", 7);
// split the URL in parts
$parts = explode("/", $url);
// The second part (offset 1) is the part we look for
if (count($parts) > 1) {
$segment = $parts[1];
} else {
throw new Exception("Full URLs please!");
}
答案 1 :(得分:2)
$url = "http://www.example.co.uk/elderly-care-advocacy/mental-capacity-act-advance-medical-directive.html";
$parts = parse_url($url);
$host = $parts['host'];
$path = $parts['path'];
$items = preg_split('/\//',$path,null,PREG_SPLIT_NO_EMPTY);
$firstPart = $items[0];
答案 2 :(得分:1)
脱离我的头顶:
$url = http://www.example.co.uk/elderly-care-advocacy/mental-capacity-act-advance-medical-directive.html
$urlParts = parse_url($url); // An array
$target_string = $urlParts[1] // 'elderly-care-advocacy'
干杯
答案 3 :(得分:1)
explode('/', $a);
答案 4 :(得分:1)
所有你应该做的,首先解析网址,然后爆炸字符串并获得第一部分。通过一些健全性检查会像下面这样:
$url = 'http://www.?.co.uk/elderly-care-advocacy/mental-capacity-act-advance-medical-directive.html';
$url_parts = parse_url($url);
if (isset($url_parts['path'])) {
$path_components = explode('/', $ul_parts['path']);
if (count($path_components) > 1) {
// All is OK. Path's first component is in $path_components[0]
} else {
// Throw an error, since there is no directory specified in path
// Or you could assume, that $path_components[0] is the actual path
}
} else {
// Throw an error, since there is no path component was found
}
答案 5 :(得分:0)
我认为Regular Expression
应该没问题。
尝试使用例如:/[^/]+/
,它应该为您提供/elderly-care-advocacy/
作为示例中数组的第二个索引。
(第一个字符串是/ www。?。com /)
答案 6 :(得分:0)
Parse_URL是您的最佳选择。它将URL字符串分解为组件,您可以选择性地查询它们。
可以使用此功能:
function extract_domain($url){
if ($url_parts = parse_url($url), $prefix = 'www.', $suffix = '.co.uk') {
$host = $url_parts['host'];
$host = str_replace($prefix,'',$host);
$host = str_replace($suffix,'',$host);
return $host;
}
return false;
}
$host_component = extract_domain($_SERVER['REQUEST_URI']);
答案 7 :(得分:0)
我也很惊讶,但这很有效。
$url='http://www.?.co.uk/elderly-care-advocacy/...'
$result=explode('/',$url)[3];