简体   繁体   中英

remove part of the segment from url laravel

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.

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

$segments = Request::segments();
$first = array_shift($segments);
if (some_condition) {
    return redirect()->to(implode('/', $segments));
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM