php - Laravel Blade 组件 - UTF-8 编码问题

标签 php laravel encoding

我目前正在开发一个 Laravel 8 版本的应用程序。我构建了一个名为 input-group 的组件这会导致我不明白的编码问题。该组件的代码如下所示:

<div class="form-group" {{ isset($attributes['style']) ? $attributes->merge(['style' => $attributes['style']]) : null }}>
    @if(isset($attributes['label']))
        <label for="{{ $attributes['id'] }}">{{ $attributes['label'] }}</label>
        <input type="text" 
               value="{{ isset($attributes['value']) ? $attributes['value'] : null }}" 
               class="form-control form-control-sm" 
               name="{{ $attributes['name'] }}" 
               id="{{ $attributes['id'] }}" 
               placeholder="{{ isset($attributes['placeholder']) ? $attributes['placeholder'] : null }}">
    @else 
        <input style="width:100%;" type="text" value="{{ isset($attributes['value']) ? $attributes['value'] : null }}" class="form-control form-control-sm" name="{{ $attributes['name'] }}" id="{{ $attributes['id'] }}" placeholder="{{ isset($attributes['placeholder']) ? $attributes['placeholder'] : null }}">
    @endif
  </div>

这是我注入(inject) value 的数据属性=> Inspecteur de l'Education Nationale

这是我在 <input> 中得到的输出:Inspecteur de l&#039;Education Nationale

我精确地说,我的数据库(使用 mySQL)具有 Laravel 默认编码 utf8mb4_unicode_ci 。我用来获取这些数据的请求是一个经典的 Eloquent 请求(我不操纵编码)

mysql 驱动程序的配置如下所示:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

最佳答案

{!! !!} 可以工作,但我不会在这里使用它。

特别是如果您正在呈现的值有可能被用户设置,因为它是未转义的。这是一个潜在的漏洞,您很容易受到 XSS 攻击。

你可以这样做:

{{ html_entity_decode($value) }},您仍然可以获得 Blade 帮助防止 XSS 攻击的好处。

您可以在此处阅读有关该功能的更多信息:https://www.php.net/manual/en/function.html-entity-decode.php .

关于php - Laravel Blade 组件 - UTF-8 编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69840689/

相关文章:

php - 从 iPhone sdk 上的 Cocoa Touch 发送变量到 php

javascript - PHP json_encode 输出带引号的数字

php - 在 Laravel 中使用 Eloquent 多态关系对数据进行分类

c++ - Windows Media 基金会 : How to set an encoder property?

sql - POSTGRESQL: 'utf8' 编解码器无法解码位置 31 中的字节 0x82:无效的起始字节

python - 如何在 Python 3.2 或更高版本中使用 'hex' 编码?

javascript - 如何查询数据库并动态绘制仪表板上的值?

javascript - PHP 中的 CSS 样式在 "onclick"脚本后丢失 — 需要帮助保持 HTML 表格具有原始 CSS 样式

PHPUnit 数据提供程序参数计数错误

css - Laravel 中的样式输入类型文件