Ken2k7 Posted August 20, 2007 Share Posted August 20, 2007 Is there a way to get the URL of a page without actually typing it out and then store that in a variable $url? If so, how can I strip the URL? For example: say the URL is: www.blah.com/ken2k7/page1.php Say I want to get the ken2k7 part and store that into a variable $user, how would I do that? And the URL won't always be that, it can be www.blah.com/newuser/page2.php I just need an efficient way to get the name. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/ Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 do you mean this $data = "www.blah.com/ken2k7/page1.php"; if (preg_match('%www\.blah\.com/([^/]*)([^/]*)%i', $data, $regs)) { $result = $regs[1]; } echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/#findComment-329310 Share on other sites More sharing options...
Ken2k7 Posted August 20, 2007 Author Share Posted August 20, 2007 Thanks for the quick reply, MadTechie. Uh, just a few questions on that. 1. What's $regs? 2. Can this not be a conditional statement? Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/#findComment-329313 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 infact thats wrong the reason for the conditional statement is preg_match will return the number of match's while $regs returns the array.. so i format them like this if (preg_match('%www\.blah\.com/([^/]*)(.*)%i', $data, $regs)) { $path = $regs[1]; $page = $regs[2]; } else { $path= ""; $page = ""; } echo $path; echo $page; Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/#findComment-329319 Share on other sites More sharing options...
Ken2k7 Posted August 20, 2007 Author Share Posted August 20, 2007 infact thats wrong the reason for the conditional statement is preg_match will return the number of match's while $regs returns the array.. so i format them like this if (preg_match('%www\.blah\.com/([^/]*)(.*)%i', $data, $regs)) { $path = $regs[1]; $page = $regs[2]; } else { $path= ""; $page = ""; } echo $path; echo $page; Ah, I see. Thanks. My last question will be is there a way php can get the URL of the page without me typing '%www.\.blah\.com etc? Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/#findComment-329324 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 i assume you mean the URL of the current view page.. simpler code if so then <?php $cPage = $_SERVER["PHP_SELF"]; $parts = explode("/",$cPage); $page = $parts[count($parts)-1]; $path = $parts[count($parts)-2]; echo $path."~~".$page; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/#findComment-329331 Share on other sites More sharing options...
Ken2k7 Posted August 20, 2007 Author Share Posted August 20, 2007 Perfect! MadTechie, you're the best! Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/#findComment-329337 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 can you please click topic solved (save other clicking here to help, only to find its solved) Quote Link to comment https://forums.phpfreaks.com/topic/65880-solved-url-strip/#findComment-329342 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.