Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a port number from a string

Tags:

php

How do you remove the port number from a url string? For example:

$string = "http://www.url.com:88";

or

$string = "http://www.url.net:88";

and so on.

I tried:

$string = substr($string, 0, strpos($string, ":", 0));

but that removes the first ":" and not the second.

like image 776
lancey Avatar asked Dec 08 '22 00:12

lancey


1 Answers

You can use parse_url for that.

$urlParts = parse_url("http://www.url.net:88");
print_r($urlParts);

this will output:

Array
(
    [scheme] => http
    [host] => www.url.net
    [port] => 88
)

then unset the port with unset($urlParts['port']) and glue it back together.

Also see http://php.net/manual/en/function.parse-url.php

like image 112
John Bakker Avatar answered Jan 02 '23 23:01

John Bakker