如何删除视图中的空格?

How can I remove spaces in view?

我用laravel 5.3

我的情况是这样的:

我的代码在视图中:

<div class="alert alert-warning" role="alert">
    "
    @if(isset($country))
        {{ $country }}
    @endif
    @if(isset($city))
        @if(isset($country))
            -
        @endif
        {{ $city }}
    @endif
    "
</div>

结果是这样的:

" England - London "

我想要这样的结果:

"England - London"

我该怎么做?

您试过使用PHP的trim功能吗? http://php.net/manual/en/function.trim.php

<div class="alert alert-warning" role="alert">
    <span>"</span>
    @if(isset($country))
        {{ $country }}
    @endif
    @if(isset($city))
        @if(isset($country))
            -
        @endif
        {{ $city }}
    @endif
    <span>"</span>
</div>

这不是最好的方法,但您可以尝试将字符串设置为变量并尝试以下代码。请验证控制器中的变量是否不包含空格,并将修剪后的变量发送到视图

<div class="alert alert-warning" role="alert">
    {{--*/ $finalString = ""; /*--}}
    @if(isset($country))
        {{--*/ $finalstring .= $country; /*--}}
    @endif
    @if(isset($city))
        @if(isset($country))
            {{--*/ $finalString .= ' - '; /*--}}
        @endif
        {{--*/ $finalString .= $city; /*--}}
    @endif
    "{{ $finalString }}"
</div>

您可以使用:

@isset($country)
    @php($result = $country)
 @endisset
 @isset($city)
     @isset($country)
         @php($result .= '-')
     @endisset
         @php($result .= $city)
 @endisset
{{$result}}

祝你好运!