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 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. 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. 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. 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 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
Archived
This topic is now archived and is closed to further replies.