Mcod Posted September 8, 2011 Share Posted September 8, 2011 I have a database full of strings which are basically URL's with ports, so by default my strings look like: http://example.com:1234/ http://example2.com:3030/ http://example.com:9876/ http://example.com:5432/ What I am looking for is a way to separate the host from the post so the output would be something like: Host: example.com Port: 1234 Note: I once need to check one string at a time, so what I am after is something like: $string = "http://example.com:1234/"; And the result would be something like: $host (would be example.com without http://) $port (would be the port without ending /) Any help would be great Thanks for your time reasing this. Quote Link to comment https://forums.phpfreaks.com/topic/246703-splitting-string-into-two-parts/ Share on other sites More sharing options...
Muddy_Funster Posted September 8, 2011 Share Posted September 8, 2011 do you want to do it in php or sql? Quote Link to comment https://forums.phpfreaks.com/topic/246703-splitting-string-into-two-parts/#findComment-1266813 Share on other sites More sharing options...
Adam Posted September 8, 2011 Share Posted September 8, 2011 You can do this easily with explode (and rtrim() to remove the trialling slash): list($host, $port) = explode(':', rtrim($string, '/')); Edit Didn't spot the "without http://" comment, use this instead: $string = str_replace('http://', '', $string); $string = rtrim($string, '/'); list($host, $port) = explode(':', $string); Quote Link to comment https://forums.phpfreaks.com/topic/246703-splitting-string-into-two-parts/#findComment-1266823 Share on other sites More sharing options...
Mcod Posted September 8, 2011 Author Share Posted September 8, 2011 Thanks for your replies. @adam, The $host part works fine and returns the host, but $port doesn't return anything for some reason (using your edited code). Any idea what might cause the port part to stay empty? Edit, I just noticed that my sql query returned a sample record where no port was existing - sorry about this false statement - the code is fine as posted - Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/246703-splitting-string-into-two-parts/#findComment-1266859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.