tags:

views:

144

answers:

7

Take this domain:

http://www.?.co.uk/elderly-care-advocacy/mental-capacity-act-advance-medical-directive.html

How could i use PHP to find the everything between the first and second slash regardless of whether it changes or no?

Ie. elderly-care-advocacy

Any helo would be greatly appreciated.

A: 

I think a Regular Expression should be fine for that.

Try using e.g.: /[^/]+/ that should give you /elderly-care-advocacy/ as the second index of an array in your example.

(The first string is /www.?.com/)

ApoY2k
Woudld't that give you www.?.co.uk as the first match? What you're looking for would be the second match.
C. Ross
I just added that, but IIRC php returns an array, so taking the $arr[1] should do well.
ApoY2k
+1  A: 

off the top of my head:

$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'

Cheers

Dashman
this code does not work.
Peter Lindqvist
+1  A: 

explode('/', $a);

chelmertz
+2  A: 
//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!");
}
Boldewyn
if the url is https://* then this will return the host part of the url
Peter Lindqvist
Thanks for this, worked a treat! And thanks to everyone else for your posts to help me out.
Andy
@Peter: Yes, I said so in the comment.
Boldewyn
... but that is easily fixed: Either you skip the substr part alltogether and look lateron for the 3rd element or you use preg_replace or similar. But if you just have plain HTTP, this is IMHO the fastest way.
Boldewyn
Agreed with Boldewyn, this should do it: `preg_replace('~(ht|f)tps?://~', null, $url)`
chelmertz
A: 

All you should do, is parse url first, and then explode string and get first part. With some sanity checks that would lok like following:

$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
}
Kaspars Foigts
+1  A: 
$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];
Peter Lindqvist
A: 

Parse_URL is your best option. It breaks the URL string down into components, which you can selectively query.

This function could be used:

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']);
Ben Werdmuller