如何删除视图中的空格?

时间:2017-06-07 08:33:55

标签: php laravel laravel-5.3 blade laravel-blade

我使用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>

结果如下:

  

“英格兰 - 伦敦”

我想要这样的结果:

  

“英格兰 - 伦敦”

我该怎么做?

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以使用:

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

祝你好运!

相关问题