frobak Posted December 9, 2011 Share Posted December 9, 2011 Hi I need to take the number off of the end of a url and do some processing with it. so for example: www.something.com/something/10001 i will need to seperate them into 2 different variables, for example: $var1 = www.something.com/something/ $var2 = 10001 is this possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/ Share on other sites More sharing options...
ialsoagree Posted December 9, 2011 Share Posted December 9, 2011 Does the number always appear after a slash "/"? Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296209 Share on other sites More sharing options...
frobak Posted December 9, 2011 Author Share Posted December 9, 2011 yes Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296210 Share on other sites More sharing options...
ialsoagree Posted December 9, 2011 Share Posted December 9, 2011 $var2 = substr(strrchr(__FILE__, '/'), 1); $var1 = substr(__FILE__, 0, -(strlen($var2))); I believe this will work, but it's untested, I'm just trying to work it out in my head so I strongly suggest testing it first and if it's not giving you what you expect, report back on your results. Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296216 Share on other sites More sharing options...
frobak Posted December 9, 2011 Author Share Posted December 9, 2011 great thanks, ill test and report back. Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296217 Share on other sites More sharing options...
frobak Posted December 9, 2011 Author Share Posted December 9, 2011 hang on, sorry. So how would i input the url to be processed by that code? I assume the url is within the "_FILE_", so how do i get it into that? can i assign like a normal variable? Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296221 Share on other sites More sharing options...
ialsoagree Posted December 9, 2011 Share Posted December 9, 2011 __FILE__ is a global constant defined by PHP. It should contain the entire URL and, iirc, any get variables (get variables WILL break this code iirc, please let me know if that's a problem). Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296223 Share on other sites More sharing options...
frobak Posted December 9, 2011 Author Share Posted December 9, 2011 so i have a form with an input, and a user will enter the full url into this input, and submit the form. How do i catch the user inputted url and get that into _FILE_? Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296224 Share on other sites More sharing options...
ialsoagree Posted December 9, 2011 Share Posted December 9, 2011 Actually, __FILE__ might not do what I'm thinking. In any event, your 2nd problem is more straight forward. You can simply replace __FILE__ with wherever the user input is stored... IE. if it was submitted in a text box named "url" you'd replace __FILE__ with $_POST['file'] But you should make sure to validate user input in advance! $var2 = substr(strrchr($_POST['url'], '/'), 1); $var1 = substr($_POST['url'], 0, -(strlen($var2))); Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296225 Share on other sites More sharing options...
frobak Posted December 9, 2011 Author Share Posted December 9, 2011 ialsoagree, thanks for that. worked like a treat! Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296242 Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 You might be able to make a more dynamic solution from this: function segment($index = 1) { $segments = explode ('/', $_SERVER['REQUEST_URI']); if (!empty($segments)) { array_shift($segments); return $segments[$index - 1]; } return null; } To use, simply count the number of "segments" after your domain and there you go. So in "www.something.com/something/10001", doing echo segment(2) would echo "10001". EDIT: By the way I didn't rigorously test this, just a push in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296246 Share on other sites More sharing options...
sphinx Posted December 9, 2011 Share Posted December 9, 2011 Frobak, your site contact form is flawed. I can post blanks. Quote Link to comment https://forums.phpfreaks.com/topic/252829-seperate-number-from-end-of-url/#findComment-1296247 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.