moses toh
moses toh

Reputation: 13192

How can I remove spaces in view?

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?

Upvotes: 0

Views: 3297

Answers (4)

Dang Cong Duong
Dang Cong Duong

Reputation: 506

You can use:

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

Good luck!

Upvotes: 0

linktoahref
linktoahref

Reputation: 7992

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>

Upvotes: 1

Rahul
Rahul

Reputation: 2474

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

Upvotes: 0

Thomas James Tiam-Lee
Thomas James Tiam-Lee

Reputation: 691

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

Upvotes: 0

Related Questions