harunB10
harunB10

Reputation: 5197

Remove empty space in string

I have a string which contains a list of tags. Each tag redirects to another page. For example PS4, Xbox360, PC But it also passes the empty space as %20Xbox360. How can I remove this %20?

This is how I render tags:

@foreach($niz as $n)
   <a href="/search?q={{$n}}" class="tags">{{$n}}{{$loop->last ? '' : ','}}</a>
@endforeach

Upvotes: 1

Views: 2853

Answers (1)

ishegg
ishegg

Reputation: 9927

%20 is just whitespace url-encoded. So, urldecode() it, then trim() it:

<?php
$string = "%20Xbox360";
$string = urldecode($string);
$string = trim($string);
var_dump($string); // Xbox360

Demo

And to do this while echoing in Blade, just do it inside the curly braces:

<a href="/search?q={{ trim(urldecode($n)) }}" class="tags">{{$n}}{{$loop->last ? '' : ','}}</a>

Upvotes: 6

Related Questions