tags:

views:

46

answers:

2

How would I go about extracting just the base URl from a long string that was inputted into a form?

Example: User inputs: http://stackoverflow.com/question/ask/asdfasneransea Program needs just: http://stackoverflow.com and not /question/ask/asdfasneransea

What is the easist way to do this using PHP?

+5  A: 

You can simply use parse_url()

$url = parse_url('http://example.com/foo/bar');
$host = $url['host']; // example.com
Greg
+1  A: 

Use the parse_url function to get the separate parts of the URL, then reassemble the parts you are looking for.

NineBerry