FareedR's avatar

How can I ignore " + " in blade for the last index of array ?

// database
id | signal |
1     PLT+PLG 

// blade
@php 
    $signals = explode('+', $stock->signal->signal);
@endphp
@if(count($signals) > 1)
    @foreach($signals as $signal) 
        {{ $signal }} <br/> + <br/>
    @endforeach
@else
	{{ $signals[0] }} 
@endif

// current result 
PLT
+
PLG
+

// expected result
PLT
+
PLG
0 likes
4 replies
rodrigo.pedra's avatar
Level 56

Try this:

{!! str_replace('+', '<br /> + <br />', $stock->signal->signal) !!}

The {!! ... !!} won't escape the output

Also you might not even need to handle the special case of one item in the list

FareedR's avatar

from a 10++ LoC become 1 LoC . like a charm . thank you for your response !

rodrigo.pedra's avatar

If you need to escape each token, try this:

{!! implode('<br /> + <br />', array_map('e', explode('+', $stock->signal->signal))) !!}

The e (...) helper is available globally and will escape each token from the explode function.

Please or to participate in this conversation.