简体   繁体   中英

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

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

This should do the trick.

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;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM