Jump to content

[SOLVED] Selecting a needle out of a haystack


pianoman993

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.