简体   繁体   English

从url laravel中删除部分细分

[英]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. 如果符合特定条件,我想删除$ lang段。 I'm retieving the url like this 我正在像这样回复网址

$url = Request::fullUrl();

Now I want to reconstruct the url to something like this 现在我想将url重建为这样的东西

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. 请注意我不能使用str_replace,因为它搜索url的所有部分,我只想在第一个段中搜索和替换。

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): 如果你只是想快速检查,然后删除第一个段并重定向用户,你可以很容易地做到这样(对于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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