【问题标题】:PHP - Drupal create node utf8 romanian charactersPHP - Drupal 创建节点 utf8 罗马尼亚字符
【发布时间】:2017-09-21 11:02:28
【问题描述】:

我有一个每天创建一个新节点的 cronjob。正文值是罗马尼亚语,因此文本包含罗马尼亚语变音符号。

DB 列都是 utf8-general-ci。 (两个表:我从和 drupal 的字段正文表中获取数据的那个)。

我正在使用此代码来创建节点:

$new_node = new stdClass();
$new_node->type = 'quote_of_the_day';
node_object_prepare($new_node);

$new_node->language = 'ro';
$new_node->uid = USER_ID;

$new_node->title = $citat['titlu'];
$new_node->body['und'][0]['value'] = $citat['text'];

$new_node->body['und'][0]['format'] = 'full_html';
$new_node->body['und'][0]['safe_value'] = $citat['text'];

我的问题是罗马尼亚字符被一些奇怪的字符取代。见图片:

我在此节点类型上将多语言设置为 true。当我编辑此脚本创建的节点时,语言设置正确。

我正在使用 drupal 7.56。

知道如何将正确的内容放入正文和浏览器吗?

编辑: 我添加了所有@M0ns1f 所说的内容。相同的输出。

【问题讨论】:

    标签: php drupal utf-8 special-characters


    【解决方案1】:

    首先你需要为HTML output..指定字符集在你的页眉中添加:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    

    PHP中你可以使用(这一行必须在你的代码的第一行):

    <?php header("Content-type: text/html; charset=utf-8");?>
    

    那么你需要在你网站的根目录中写一个.htaccess

    `# Set httpd charset to utf-8 
    AddDefaultCharset On
    AddDefaultCharset utf-8`
    

    将 php charset 设置为 utf-8 并设置 mbstring(可能需要安装 mbstring 模块)

     php_value default_charset utf-8
     php_value mbstring.internal_encoding utf-8
     php_value mbstring.func_overload 7
    

    source


    然后尝试添加mb_convert_encoding函数

    $new_node = new stdClass();
    $new_node->type = 'quote_of_the_day';
    node_object_prepare($new_node);
    
    $new_node->language = 'ro';
    $new_node->uid = USER_ID;
    
    $new_node->title = $citat['titlu'];
    $new_node->body['und'][0]['value'] = mb_convert_encoding($citat['text'], 'HTML-ENTITIES', 'UTF-8');
    
    $new_node->body['und'][0]['format'] = 'full_html';
    $new_node->body['und'][0]['safe_value'] = mb_convert_encoding($citat['text'], 'HTML-ENTITIES', 'UTF-8');
    

    或者把$citat['text']改成这个

      htmlentities(utf8_encode($citat['text']), 0, "UTF-8")
    

    【讨论】:

    • 做到了。相同的输出。
    • @sebastian.roibu 查看我在答案中所做的修改
    【解决方案2】:

    你试过了吗:

    $new_node->body['und'][0]['value'] = mb_convert_encoding($citat['text'], 'HTML-ENTITIES', mb_detect_encoding($citat['text']));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 2012-01-13
      相关资源
      最近更新 更多