regex to eliminate port number from url - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: regex to eliminate port number from url (/showthread.php?tid=68241) |
regex to eliminate port number from url - cupboy1 - 06-14-2017 I tried this and some other things. What I need in this example would be to return just localhost without the colon and the port number. There may or may not be a port number and it needs to return a value in both cases. Currently it returns 'localhost:8080' in [1][0] but I want just 'localhost' to be in there. Code: $subject1 = "localhost:8080"; RE: regex to eliminate port number from url - rtenny - 06-15-2017 If you just want to remove the port number from the URL i would do this $pattern = "/:[0-9]*/"; $url = preg_replace($pattern, '', $subject1); even if $subject1 = localhost:8080/index.php it will correctly return localhost/index.php RE: regex to eliminate port number from url - skunkbad - 06-15-2017 Technically, this is a valid URL: http://username:[email protected]:8080/one/two/three?legal=:40#:yes So you are better off parsing it with PHP's parse_url function: PHP Code: <?php |