2

I was reading this: http://php.net/manual/en/ref.strings.php however I'm looking for additional clarification.

I know in python I can call .split("...") as well as in Java.

So I found chunk_split(), str_split(), and explode(). What are the reasons for using each? From what I was reading I was under the impression that each could be used for the same task. Is there an explicit advantage for using one over the other?

I'm looking to split a url to get the "Base" (not sure what else to call it)

Ex:

http://www.stackoverflow.com/questions/ask

I was thinking I could cut off the front with if statements

if = http:// or http://www. or www. etc... #pseudo

Then cut by "/" chars and take the front.

Which function is best purposed for this?

3
  • 4
    parse_url() Commented Aug 5, 2014 at 18:20
  • Along with basename(). Not sure if you want the deepest dir or the domain name. Commented Aug 5, 2014 at 18:22
  • @AbraCadaver can you explain that? I would like the domain name. Ex stack overflow.com Commented Aug 5, 2014 at 19:53

1 Answer 1

3

The function you're after is parse_url():

<?php

$url = 'http://www.stackoverflow.com/questions/ask';

$parsed_url = parse_url($url);

echo '<pre>';
print_r($parsed_url);
echo '</pre>';

output:

Array
(
    [scheme] => http
    [host] => www.stackoverflow.com
    [path] => /questions/ask
)
Sign up to request clarification or add additional context in comments.

3 Comments

It's a typo, the important stuff is there.
@bali182: I'm well aware that it's a typo, the answerer should take more care with what they post.
I agree Blue Dog, thanks for pointing it out. I actually got pulled away from my desk right at the moment I'd normally review my answer and make edits, so there was nothing I could do. :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.