ted_chou12 Posted September 28, 2008 Share Posted September 28, 2008 Hi, how can a preg_match code that split a url into domain, page at once? I found: preg_match('@^(?:http://)?([^/]+)@i', $link, $final); This doesnt work, only get rid of the http://, I wish to come up with something that deletes "http://" and splits the rest of the url to two parts, the domain and the page: http://phpfreaks.com/forums/page12.php The result can be something like: echo $final[0] gives phpfreaks.com/ echo $final[1] gives forums/page12.php Thanks, Ted Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 28, 2008 Share Posted September 28, 2008 For things like this, you don't need regex. You can utilize parse_url() instead. $url = parse_url('http://phpfreaks.com/forums/page12.php'); echo '<pre>'; print_r($url); echo '</pre>'; output: Array ( [scheme] => http [host] => phpfreaks.com [path] => /forums/page12.php ) So in otherwords, you can simply output something like: echo $url['path']; // which outputs: /forums/page12.php Cheers, NRG EDIT: In the event you don't want the first slash in $url['path'], you can instead echo like this: echo substr($url['path'], 1, strlen($url['path'])-1); // ouputs: forums/page12.php Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted September 28, 2008 Author Share Posted September 28, 2008 Thanks, that helps a lot^^~~ Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 28, 2008 Share Posted September 28, 2008 No problem. Quote Link to comment Share on other sites More sharing options...
corbin Posted September 28, 2008 Share Posted September 28, 2008 nrg_alpha, the strlen call isn't needed since if it is excluded, the length will be assumed. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 28, 2008 Share Posted September 28, 2008 nrg_alpha, the strlen call isn't needed since if it is excluded, the length will be assumed. You're right. I stand corrected. :-\ Quote Link to comment Share on other sites More sharing options...
corbin Posted September 28, 2008 Share Posted September 28, 2008 It would work fine, just not needed. Was about to say no need for an extra function call, but then I realized something similar to strlen is probably called in the internals of substr anyway ;p. Just something that caught my eye. Not a biggie. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 28, 2008 Share Posted September 28, 2008 It's good that you caught that though I learned something new (as you can tell, I don't make use of substr() often). But know I know there's no need for that extra parameter.. makes coding-life easier for sure. Thanks again for the insight. Cheers, NRG Quote Link to comment 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.