将数组传递给 Twig 时,带有特殊字符的单词会消失

When passing an array to Twig, words with special characters are disappearing

我可以使用 mysqli 从数据库中查询数据,输出到 Twig 并创建一个 table,并且不显示带有重音符号(À、Í 等)的单词。

数组可以通过var_dump完美显示。所以 Twig 的某个地方出错了。

我有:

我试过了:

我不知道下一步该做什么,也不知道为什么它不起作用。 如果有人知道为什么整个单词都消失了,那就太好了。

模板示例:

{% if results %}
<div class="table-container" id="table-container">
    <table class="results-table" id="results-table">
        <thead>
        <tr>
            <th>{{ "First Name"|trans }}</th>
            <th>{{ "Second Name"|trans }}</T></th>
            <th>{{ "First Surname"|trans }}</th>
            <th>{{ "Second Surname"|trans }}</th>
            <th>{{ "Number"|trans }}</th>
            <th>{{ "Type"|trans }}</th>
            <th>{{ "District"|trans }}</th>
            <th>{{ "State"|trans }}</th>
            <th>{{ "City"|trans }}</th>
        </tr>
        </thead>

        <tbody class="results">
        {% for r in results %}
        <tr>
            <td>{{ r.first_name }}</td>
            <td>{{ r.middle_inital }}</td>
            <td>{{ r.surname }}</td>
            <td>{{ r.second_surname}}</td>
            <td>{{ r.number }}</td>
            <td>{{ r.type }}</td>
            <td>{{ r.district }}</td>
            <td>{{ r.state }}</td>
            <td>{{ r.city|capitalize }}</td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
{% endif %}

数组从 mysqli 查询传递到变量。教科书示例。

简答: AddCharset UTF-8 .html .php

回顾:

  • MySQL/Silex/Twig 应用程序有数据输出到 table
  • Table数据正确
  • 带有特殊字符的词没有被渲染
  • 浏览器和数据库设置为 UTF-8

所以,在四处寻找之后,我看到要检查正在发送的 HTTP header。它被设置为 windows-1252。这迫使浏览器在加载 table 时从 Unicode 切换。

我找到了一个指令放入 .htaccess 中,它会强制进行 UTF-8 编码。 AddCharset UTF-8 .html .php 在所有 HTML 和 PHP 页面上强制 UTF-8