Jump to content

Recommended Posts

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

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. :)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.