Andy
Andy

Reputation: 3021

Using PHP to find part of a URL

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.

Upvotes: 3

Views: 5621

Answers (8)

B.F.
B.F.

Reputation: 477

I was surprised too, but this works.

$url='http://www.?.co.uk/elderly-care-advocacy/...'
$result=explode('/',$url)[3];

Upvotes: 1

Ben Werdmuller
Ben Werdmuller

Reputation: 185

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

Upvotes: 0

Peter Lindqvist
Peter Lindqvist

Reputation: 10210

$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];

Upvotes: 2

Kaspars Foigts
Kaspars Foigts

Reputation: 159

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
}

Upvotes: 1

Boldewyn
Boldewyn

Reputation: 82794

//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!");
}

Upvotes: 5

chelmertz
chelmertz

Reputation: 20601

explode('/', $a);

Upvotes: 1

iamdash
iamdash

Reputation: 449

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

Upvotes: 1

F.P
F.P

Reputation: 17831

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/)

Upvotes: 0

Related Questions