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>
4
  • What do you mean by "useless"? I see no reason why this wouldn't work. Commented Jan 5, 2015 at 8:25
  • It works. My guess is $str is undefined or empty. Commented Jan 5, 2015 at 9:39
  • You may want to actually make them camel case instead ("firstName" instead of "firstname"). See the Laravel camel_case() function for that. Commented Aug 30, 2015 at 19:12
  • Related: How to capitalize first letter in Laravel Blade Commented Aug 8, 2024 at 1:57

5 Answers 5

37

Which version of Laravel did you use?

For laravel 5.x, use this

{!! str_replace('_', ' ', $str) !!}
Sign up to request clarification or add additional context in comments.

Comments

28

You can use php code in .blade.php file

try like this

<th> <?=str_replace('_', ' ', $str)?> </th>

Comments

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

Comments

5

In Laravel 8 you can use the String Helper function:

use Illuminate\Support\Str;

<th>{{ Str::replace('_', ' ', $str) }}</th>

Comments

2

You may use this

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

OR

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.