Reputation: 1864
For creating breadcrumbs i have a string as below:
$crumbs = explode("/", $_SERVER["REQUEST_URI"]);
This generates the following output:
/ sfm?dir=uploads / sfm / c4ca4238a0b923820dcc509a6f75849b / folder1 / folder2 /
How can i eliminate these first characters from the output:
/ sfm?dir=uploads / sfm /
so that it starts with the hash
Upvotes: 0
Views: 1117
Reputation: 76577
There are a few easy ways to tackle this problem.
Consider Replacing Prior to Exploding
The first could be to simply perform your str_replace()
call prior to the explode()
function :
# Explicitly replace your input
$input = str_replace($_SERVER["REQUEST_URI"],'/sfm?dir=uploads/sfm/','');
# Then explode as expected
$crumbs = explode("/", $input);
Slice Off The First Two Elements
Another option would be to simply slice your original array and remove the first two elements from it via the array_slice()
function:
# Explode your string into an array
$crumbs = explode("/", $_SERVER["REQUEST_URI"]);
# Trim off the first two elements of the array
$crumbs = array_slice($crumbs,2)
Upvotes: 4
Reputation: 51
Or you can exclude this string while exploding the main string:
$crumbs = explode("/", $_SERVER["REQUEST_URI"]);
$new_string = "";
for ($i = 0 ; $i < count ($crumbs) ; $i++){
if ($crumbs[$i] != 'sfm?dir=uploads'){
$new_string .= '/' . $crumbs[$i];
}
}
return $new_string; //This is your your string
Upvotes: 1
Reputation: 966
There's no need to remove the first characters before exploding the string. You can slice the array to remove the url parts you dont need.
Example code:
$crumbs = explode('/', '/sfm?dir=uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/folder1/folder2/');
$result = array_slice($crumbs, 3);
// Do something with result.
Your code will still work even when the first parts of the url start with something else.
Hope this helps
Upvotes: 1
Reputation: 103
With explode you created an array so I would create a string or another array with the array data I want to be displayed
$crumbs = explode("/", $_SERVER["REQUEST_URI"]);
//For string
$breadcrumbs = $crumbs[2].$crumbs[3].$crumbs[4];
Upvotes: 1
Reputation: 43594
Try the following using str_replace()
:
$input = '/ sfm?dir=uploads / sfm / c4ca4238a0b923820dcc509a6f75849b / folder1 / folder2 /';
$replace = '/ sfm?dir=uploads / sfm /';
echo str_replace($replace, '', $input);
With your code the following should work:
$crumbs = $_SERVER["REQUEST_URI"];
$replace = '/ sfm?dir=uploads / sfm /';
$crumbs = str_replace($replace, '', $crumbs);
$crumbs = explode("/", $crumbs);
A working demo can be found here: https://3v4l.org/QOm4S
Upvotes: 2