3

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.

8 Answers 8

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!");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this, worked a treat! And thanks to everyone else for your posts to help me out.
@Peter: Yes, I said so in the comment.
... 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.
Agreed with Boldewyn, this should do it: preg_replace('~(ht|f)tps?://~', null, $url)
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];

Comments

1

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

Comments

1

explode('/', $a);

Comments

1

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
}

Comments

1

I was surprised too, but this works.

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

Comments

0

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

2 Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.