2

I have a path that is separated by forward slash.

$uri = getenv('REQUEST_URI');
$uri = explode('/', $uri);
$uri = array_filter($uri);
$uri = array_merge($uri, array());

The path would end up something like:

/user/john/account

This will grab the last word in the path, 'account'.

$uri = end($uri);

How can I get the first word found after 'user' into a variable?

In this case it would be 'john', but the name can change on every new URI.

'user' always stays the same word.

Matt McManis
  • 4,475
  • 5
  • 38
  • 93
  • if you are using url rewriting then this would not be need to split. http://stackoverflow.com/questions/16388959/url-rewriting-with-php – Noman Feb 08 '17 at 10:23

5 Answers5

3
$uri = "/user/john/account";
$uri_array = explode("/", $uri);
$key = array_search("user", $uri_array);
$desired_value = array_key_exists($key+1, $uri_array) ? $uri_array[$key+1] : "error";
echo $desired_value;

Here is what you need : Note : "user" is a fixed term in array_search .. you can change it

M A SIDDIQUI
  • 2,028
  • 1
  • 19
  • 24
  • Hi, I'm trying to implement your solution. I found one small problem, if the path is not complete "/user" without 'john', and $key+1, it will throw an error. – Matt McManis Feb 08 '17 at 10:48
  • sorry its my my mistake .. its always good to check if array key exist – M A SIDDIQUI Feb 08 '17 at 10:51
  • It's working now in a test. It will take a while to set up for the pages and I'll get back to you and accept your answer. Thanks. – Matt McManis Feb 08 '17 at 11:01
1

Before $uri = end($uri) add this:

$uriArray = explode('/',$uri);  //$uriArray[0] = 'user'  //$uriArray[1] = 'john'

$name = $uriArray[1];

With this you could also get the last element after a slash

EDIT: You could also use preg_split('pattern',$string) like this:

$name = preg_split('user/',$uri);

$name would be "john/account". Then use explode again, to save it into an array, element 0 is the name

bloodscript
  • 137
  • 12
1

Edit: Use M A SIDDIQUIs answer. It is the better way to solve this problem

You can write a function for this that finds out whitch position of the uri-Array contains the word user and then returns the next position that should contain the name.

function getName($uri)
{
    $uri = explode('/', $uri);
    for($i = 0; $i < sizeof($uri); $i++){
        if($uri[$i] == "user"){
            return $uri[$i+1];
        }
    }
}

Then you just have to print it out with:

echo getName("exmaple/user/username/etc");
Yannick Huber
  • 607
  • 2
  • 16
  • 35
1

You can use this

$uri='/user/john/account';
$from1 ='/user/';
$to1 ='/';
//$uri = explode('/', $uri);

echo get_string_between($uri,$from1,$to1);

function get_string_between ($str,$from,$to) {
    $string = substr($str, strpos($str, $from) + strlen($from));
    if (strstr ($string,$to,TRUE) != FALSE) {
        $string = strstr ($string,$to,TRUE);
    }
    return $string;
}
Yannick Huber
  • 607
  • 2
  • 16
  • 35
1

Using preg_match might be more efficient in this case:

Psy Shell v0.8.1 (PHP 7.1.1-1+deb.sury.org~xenial+1 — cli) by Justin Hileman
>>> preg_match('~/user/([^/]+)/?~', '/test/user/username/tuedelue', $matches);
=> 1
>>> $matches;
=> [ 
     "/user/username/",
     "username",
   ]
>>> $matches[1];
=> "username"
>>> |

So these lines

$user = 'unknown';
if (preg_match('~/user/([^/]+)/?~', getenv('REQUEST_URI'), $matches)) {
    $user = $matches[1];
}

would solve your problem.

Tom Regner
  • 6,856
  • 4
  • 32
  • 47
  • This works well, though if I run it on any page that doesn't contain /user/ it will error. Is there a way around that? – Matt McManis Feb 08 '17 at 12:42
  • How much faster/light on resource do you think this is than M A SIDDIQUI's solution? http://stackoverflow.com/a/42110367/6806643 – Matt McManis Feb 08 '17 at 14:36
  • You would have to try that out in your code ;-), I wouldn't know for sure. I'd bet under <=5.6 the regex performs better, and a tie for php >= 7.0 (or even the array winning); If I find the time a benchmark and comment again – Tom Regner Feb 08 '17 at 19:52