如何在laravel中使用str_replace

Mun*_*eeb 8 laravel laravel-4

如何在laravel框架中使用php函数str_replace.

我在表列名称上的数组键名称,因此键具有'_',如first_name,last_name等.我想在刀片文件中删除这些'_'.我的要求是.blade.php文件中的字符串替换.

我正在尝试这个PHP代码,但它没用.

<th>{{str_replace('_', ' ', $str)}}</th>
Run Code Online (Sandbox Code Playgroud)

谢谢

Moh*_*put 15

您可以在.blade.php文件中使用php代码

试试这样

<th> <?=str_replace('_', ' ', $str)?> </th>
Run Code Online (Sandbox Code Playgroud)


小智 12

您使用的是哪个版本的Laravel?

对于laravel 5.x,请使用此选项

{!! str_replace('_', ' ', $str) !!}
Run Code Online (Sandbox Code Playgroud)


Nae*_*jaz 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
Run Code Online (Sandbox Code Playgroud)

刀片用

 $item = "Free-Active";
{{ Str::replaceArray('free-', [''], $item) }}
Run Code Online (Sandbox Code Playgroud)


Mai*_*rey 5

在 Laravel 8 中,您可以使用 String Helper 函数:

use Illuminate\Support\Str;

<th>{{ Str::replace('_', ' ', $str) }}</th>
Run Code Online (Sandbox Code Playgroud)