douye2572
2017-06-07 08:33
浏览 66
已采纳

如何删除视图中的空格?

I use laravel 5.3

My case is like this :

My code in view :

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

The result like this :

" England - London "

I want the result like this :

"England - London"

How can I do it?

图片转代码服务由CSDN问答提供 功能建议

我使用laravel 5.3

我的情况是这样的:

我的视野中的代码:

 &lt; div class =“alert alert-warning”role =“alert”&gt; 
“
  @if(isset($ country))
 {{$ country}} 
 @endif 
 @if(isset($ city))
 @if(isset($ country))
  -  
 @endif  
 {{$ city}} 
 @endif 
“
&lt; / div&gt; 
   
 
 

结果如下: \ n

“England - London”

我想要这样的结果:

\ n

“英格兰 - 伦敦”

我该怎么办?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doulangtiao4508 2017-06-07 08:57
    已采纳

    This wouldn't be the best method, but you could try setting the string to a variable and try the below code. Please verify whether the variable isn't containing spaces in your controller and send the trimmed variables to the view

    <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>
    
    点赞 打赏 评论
  • doutiaosu2310 2017-06-07 08:37

    Have you tried using the trim function of PHP? http://php.net/manual/en/function.trim.php

    点赞 打赏 评论
  • dqnrk44682 2017-06-07 08:38
    <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>
    
    点赞 打赏 评论
  • dongyan7851 2018-06-29 17:49

    You can use:

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

    Good luck!

    点赞 打赏 评论

相关推荐 更多相似问题