Correct way to analyze paths from $_SERVER['PATH_INFO']
php
Solution
Trim the "/" from the start and end of the array?
$array = explode('/', trim($_SERVER['PATH_INFO'], '/') );
Problem
I am trying to write a small RESTful Service API in PHP5.4 (for educational purpose). Therefor I need to analyze the URL with which the service is called. The way I want to do this is to strip down the URL paths from `$_SERVER['PATH_INFO']` and put them to an array. Let's assume the path info contains `/contacts/14295/`. What is the right way to put the two paths to an array like following? ``` array(2) { [0]=> string(8) "contacts" [1]=> string(5) "14295" } ``` I found at least two ways to split the path info string to an array but both ways result in an array with more then two entries. The first way I found is the explode function which returns an array with trailing empty strings (means the first and last array entry contain an empty string): ``` explode('/', $_SERVER['PATH_INFO']); ``` Then I tried the preg_split function which returns an array that contains an entry for every slash ('/'): ``` preg_split('//', $_SERVER['PATH_INFO']); ``` Both variants are it very unhandy for me to get the paths from URL. I would wonder if there is not a better way.