0

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

3
  • Will it always be the first 2 elements or will it always be / sfm?dir=uploads / sfm /? Commented May 4, 2016 at 19:24
  • it will always be: / sfm?dir=uploads / sfm / Commented May 4, 2016 at 19:25
  • str_replcace() before explode() Commented May 4, 2016 at 19:26

5 Answers 5

4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

array_slice() is the only option if the first part (in this case '/ sfm?dir=uploads / sfm /') can change from one request to the other.
2

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

Comments

1

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];

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.