Reputation:
As the title says I need to return a particular part of string that falls between two substrings.
Example:
$string = numbergrid_21372566/_assets/audio/
Now every time, I need to return the part of string that falls between the last two slashes (/) I.E audio in this case.
How can I achieve that? Thanks for reading
Upvotes: 0
Views: 496
Reputation: 14
If you know that it always would be the last part, you can use array_pop function:
$arr = explode('/', $string);
$result = array_pop($arr);
Upvotes: 0
Reputation: 5092
You could also use the substr method, combined with the strpos method:
$start = strpos($string, "/");
$end = strpos($string, "/", $start);
$length = $end - $start;
$result = substr($string, $start, $length);
Upvotes: 0
Reputation: 725
$arr = explode('/',$string);
$firstSegment = $arr[0]; // numbergrid_21372566
$secondSegment = $arr[1]; // _assets
$thirdSegment = $arr[2]; // audio
Upvotes: 0
Reputation: 152304
Just try with:
$string = 'numbergrid_21372566/_assets/audio/';
$output = explode('/', $string)[2];
Upvotes: 0
Reputation: 20753
Try-
$parts = explode("/", $string);
$res = $parts[count($parts)-2];
Upvotes: 0