Jamie Warren
Jamie Warren

Reputation: 45

Trying to get the host and port out of a url in php

Hi all hope this makes sense.

I am wondering if there is a way to split the following with php

so

$url = "http://50.7.71.219:7183/listen.mp3";

I then need to split it into 2 more strings so i should get a result of :

$ip = "http://50.7.71.219";
$port = "7183";

Thanks, Jamie

Upvotes: 1

Views: 1553

Answers (2)

Neil
Neil

Reputation: 14321

There is actually a built in way of doing this in PHP. Use the parse_url function. This method also returns a host of other information that could be useful, click here to learn more.

<?php

$url = "http://50.7.71.219:7183/listen.mp3";
$parsedUrl = parse_url($url);
$ip = $parsedUrl['host'];
$port = (string) $parsedUrl['port'];

echo $ip . "<br />" . $port;

?>

Upvotes: 4

Grimnis Hvern
Grimnis Hvern

Reputation: 26

$s1=explode($url,":");
$s2=explode($s1[2],"/");
$ip=$s1[0].':'.$s1[1];
$port=$s2[0];

This should do the trick.

Upvotes: 1

Related Questions