pianoman993 Posted December 29, 2008 Share Posted December 29, 2008 Hello there PHP gods, I have a small question for you. I have this string from a URL: user=/users/username/20081229_11/ Now I would like another variable called username to be equal to what is is after the /users/ and before the /20081229_11/. Is that possible? If so could someone show me what the code for that operation would look like? I am drawing a complete blank. Thanks! - Pianoman993 Quote Link to comment https://forums.phpfreaks.com/topic/138791-solved-selecting-a-needle-out-of-a-haystack/ Share on other sites More sharing options...
premiso Posted December 29, 2008 Share Posted December 29, 2008 explode $users = explode("/", $_GET['users']); $users = $users[1]; Should get you the result. Quote Link to comment https://forums.phpfreaks.com/topic/138791-solved-selecting-a-needle-out-of-a-haystack/#findComment-725704 Share on other sites More sharing options...
RussellReal Posted December 29, 2008 Share Posted December 29, 2008 a little more complex but still simplistic: <? $u = $_GET['user']; $s = 'users/'; $st = strpos($u,$s,0) + $s; $userName = substr($u,$st,strpos($u,'/',$st) - $st); ?> what that does is it finds 'users/' and returns the data after it.. but all before the next '/' which would be the full username.. but premiso's works just as good. Quote Link to comment https://forums.phpfreaks.com/topic/138791-solved-selecting-a-needle-out-of-a-haystack/#findComment-725742 Share on other sites More sharing options...
DarkWater Posted December 29, 2008 Share Posted December 29, 2008 Even better, although rather unorthodox. <?php echo basename(dirname('/users/foo/20081229_11/')); ?> Doesn't use any arrays that way. On that note, you could improve premiso's by just doing: <?php list(null, $user) = explode('/', '/users/foo/20081229_11/'); echo $user; ?> The first one is still probably a little faster. I didn't test though. Quote Link to comment https://forums.phpfreaks.com/topic/138791-solved-selecting-a-needle-out-of-a-haystack/#findComment-725747 Share on other sites More sharing options...
pianoman993 Posted December 30, 2008 Author Share Posted December 30, 2008 Thanks everyone you were all very helpful Quote Link to comment https://forums.phpfreaks.com/topic/138791-solved-selecting-a-needle-out-of-a-haystack/#findComment-725835 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.