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

时间:2015-05-07 21:37:36

标签: php mysql mysqli twig non-ascii-characters

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

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

我有:

  • 包含西班牙语数据的数据库
  • PHP
  • 捷希凯/枝条
  • utf8中的表架构,文本正确显示。

我尝试过:

    发送给Twig之前
  • mb_convert_encoding
  • convert_encoding('iso-8859', 'UTF-8')在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查询传递给变量。教科书示例。

1 个答案:

答案 0 :(得分:0)

简答: AddCharset UTF-8 .html .php

<强>回顾:

  • MySQL / Silex / Twig应用程序具有输出到表的数据
  • 表数据正确
  • 未呈现包含特殊字符的字词
  • 浏览器和数据库设置为UTF-8

所以,经过一番探讨后,我看到要检查发送的HTTP header。它被设置为windows-1252。这种情况迫使浏览器在加载表时切换到Unicode。

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