Calling a native PHP function in Laravel 4 blade doesn't work
Asked Answered
R

5

21

How can I call a native PHP function str_replace() in my Laravel 4 blade file?

I want to convert my snake_case values to space-separated words using str_replace().

I am trying this php code but it's useless.

<th>{{str_replace('_', ' ', $str)}}</th>
Roodepoortmaraisburg answered 5/1, 2015 at 6:45 Comment(4)
What do you mean by "useless"? I see no reason why this wouldn't work.Dropping
It works. My guess is $str is undefined or empty.Mailemailed
You may want to actually make them camel case instead ("firstName" instead of "firstname"). See the Laravel camel_case() function for that.Kandykane
Related: How to capitalize first letter in Laravel BladeLuciferin
S
28

You can use php code in .blade.php file

try like this

<th> <?=str_replace('_', ' ', $str)?> </th>
Saltish answered 5/1, 2015 at 6:47 Comment(0)
L
37

Which version of Laravel did you use?

For laravel 5.x, use this

{!! str_replace('_', ' ', $str) !!}
Letrice answered 2/12, 2015 at 6:27 Comment(0)
S
28

You can use php code in .blade.php file

try like this

<th> <?=str_replace('_', ' ', $str)?> </th>
Saltish answered 5/1, 2015 at 6:47 Comment(0)
B
5
use Illuminate\Support\Str;

$string = 'The event will take place between ? and ?';

$replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);

// The event will take place between 8:30 and 9:00

For Blade

 $item = "Free-Active";
{{ Str::replaceArray('free-', [''], $item) }}
Baby answered 5/9, 2019 at 11:47 Comment(0)
L
5

In Laravel 8 you can use the String Helper function:

use Illuminate\Support\Str;

<th>{{ Str::replace('_', ' ', $str) }}</th>
Lickerish answered 23/10, 2021 at 9:17 Comment(0)
C
2

You may use this

<th>{{-- */ echo str_replace('_', ' ', $str); /* --}}</th>

OR

<th>{{-- */ $data = str_replace('_', ' ', $str); /* --}} {{$data}}</th>
Cruck answered 31/3, 2015 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.