3

I have a url like this:

http://example.com/en/search

Now I want to check the first segment of the url like

$lang = Request::segment(1);

And I want to remove the $lang segment if it matches to the certain conditions. I'm retieving the url like this

$url = Request::fullUrl();

Now I want to reconstruct the url to something like this

http://example.com/search

Please note I cannot use str_replace because it searches in all parts of the url, I just want to search and replace in the first segment.

2 Answers 2

6

If you just want to make a quick check, then remove the first segment and redirect the user, you could easily do e.g. like this (for Laravel 5):

$segments = Request::segments();
$first = array_shift($segments);
if (some_condition) {
    return redirect()->to(implode('/', $segments));
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try something like:

$str = 'http://example.com/en/search';
$part = explode('/',$str,5);

if ($part[3] == 'en') {
    $url = $part[0].'//'.$part[2].'/'.$part[4];
} else { 
    $url = $str;
}

echo '$url:['.$url.']';  // http://example.com/search

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.