在mysql中序列化php存储多语言数据

发布于 2024-09-08 07:56:32 字数 1730 浏览 5 评论 0原文

我必须将一些数据插入 MySQL 表中。将检索数据,然后(目前)反序列化,此时将选择正确的显示语言...

我已设法将数据(用 markdown 编码的文本)合并到一个集合中PHP语句,大致如下:

<?php
$component_data = array();
$component_data[65] =
  array( // reformatted to avoid side-scrolling
      "en"=>"* Student welfare is our top priority.\n* We 
               have 30 years of experience of running successful 
               courses for Young Learners.",
      "es"=>"* El bienestar de nuestros estudiantes es nuestra 
               principal prioridad.\n* Contamos con experiencia de 
               30 años de exitosa realización de cursos para jóvenes.",
      "de"=>"* Das Wohl des Lernenden ist unsere oberste Priorität.\n 
               *Wir organisieren seit 30 Jahren erfolgreich 
               Sprachkurse für Jugendliche",
      "it"=>"* Il benessere degli studenti è la nostra priorità 
               assoluta.\n* Abbiamo 30 anni di esperienza nei corsi 
               per ragazzi.",
      "fr"=>"* Le bien-être de l’élève a pour nous la priorité absolue.
             \n* Nous avons 30 ans d'expérience dans la gestion de cours 
             réussis pour jeunes étudiants");
?>

我希望使用以下内容将其转换为准备导入MySQL表的格式:

<?php
    foreach ($component_data as $id => $value) {
      echo "UPDATE `components` SET `component_content`='".
        mysql_real_escape_string(serialize($value)).
        "' WHERE `id` = '$id';\n";
    }
?>

不幸的是它确实进入了,但页面上的结果被破坏了,即它只是显示序列化的字符串,而不是数组(如果无法设法反序列化从 MySQL 获取的字符串,则这是默认行为)。

我已经尝试了 PHP 字符串清理函数的多种排列,坦率地说,我的头在旋转。

理想情况下,我希望能够重新格式化 PHP 样式的数据以插入 MySQL 数据库,以便在获取时它仍然处于不可序列化状态......

并获得奖励积分,如果你可以将utf8外语字符转换为HTML实体并从markdown转换为HTML

I have to insert some data into a MySQL table. The data will be retrieved and then (at present) unserialized at which point the correct display language will be selected...

I've managed to munge the data (text encoded with markdown) into a set of PHP statements, roughly along the following lines:

<?php
$component_data = array();
$component_data[65] =
  array( // reformatted to avoid side-scrolling
      "en"=>"* Student welfare is our top priority.\n* We 
               have 30 years of experience of running successful 
               courses for Young Learners.",
      "es"=>"* El bienestar de nuestros estudiantes es nuestra 
               principal prioridad.\n* Contamos con experiencia de 
               30 años de exitosa realización de cursos para jóvenes.",
      "de"=>"* Das Wohl des Lernenden ist unsere oberste Priorität.\n 
               *Wir organisieren seit 30 Jahren erfolgreich 
               Sprachkurse für Jugendliche",
      "it"=>"* Il benessere degli studenti è la nostra priorità 
               assoluta.\n* Abbiamo 30 anni di esperienza nei corsi 
               per ragazzi.",
      "fr"=>"* Le bien-être de l’élève a pour nous la priorité absolue.
             \n* Nous avons 30 ans d'expérience dans la gestion de cours 
             réussis pour jeunes étudiants");
?>

and I was hoping to use the following to get it into a format ready for import into the MySQL table:

<?php
    foreach ($component_data as $id => $value) {
      echo "UPDATE `components` SET `component_content`='".
        mysql_real_escape_string(serialize($value)).
        "' WHERE `id` = '$id';\n";
    }
?>

Unfortunately it does go in, but the result on the page is mangled, i.e. it just shows the serialised string, rather than the array (which is the default behaviour if it can't managed to unserialise the string fetched from MySQL).

I've tried a number of permutations of the PHP string cleaning functions, and my head is frankly spinning.

Ideally, I'd like to be able to reformat the PHP styled data for insertion into the MySQL db, so that when fetched it's still in an unserializable state...

... and for bonus points, if you can convert the utf8 foreign language chars to HTML entitities and from markdown into HTML

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-柠檬树下少年和吉他 2024-09-15 07:56:32

您是否尝试删除“mysql_real_escape_string”以查看反序列化是否有效?

您可以尝试的另一件事是对序列化数组进行 Base64 编码。

<?php
    foreach ($component_data as $id => $value) {
      echo "UPDATE `components` SET `component_content`='".
        base64_encode(serialize($value)).
        "' WHERE `id` = '$id';\n";
    }
?>

然后对其进行 base64_decode 并在检索时反序列化。

Have you tried removing the 'mysql_real_escape_string' to see if the unserialize works?

Another thing you could try is base64 encoding on the serialised array.

<?php
    foreach ($component_data as $id => $value) {
      echo "UPDATE `components` SET `component_content`='".
        base64_encode(serialize($value)).
        "' WHERE `id` = '$id';\n";
    }
?>

And then base64_decode it and unserialise when you retrieve it.

暮年 2024-09-15 07:56:32

感谢大家提出的有用建议。

实际上,问题出在存储的texture(不是markdown!)和多语言utf-8的混合上。将其压缩到 MySQL 中的解决方案有点粗糙。首先对数据集运行 Textile 以将其转换为 html,然后通过以下内容对其进行 munge,以处理外来字符的编码:

<?php
include 'data.php'; // contains component data similar to above.

foreach ($component_data as $id => $value) {
  foreach ($value as $language => $translation) {
    $value[$language] = str_replace(
      array("<",">"),
      array('<','>'), 
      htmlentities($translation, ENT_NOQUOTES, "UTF-8")
      );
  }
  echo "UPDATE `components` SET `component_content`='".mysql_real_escape_string(serialize($value))."' WHERE `id` = '$id';\n";
}

?>

重要的一点是 ENT_NOQUOTES,这意味着一个简单的 str_replace code> 可以处理开始和结束标记(幸好文本中没有数学),并且 mysql_real_escape_string 可以处理单引号。很高兴一切都结束了。

Thanks to everyone for their useful suggestions.

Actually, the problem turned out to be the mixture of stored textile (not markdown!) and multi-lingual utf-8. The solution to squeezing it into MySQL was a bit crufty. First run textile over the dataset to get it into html, and then munge it through the following, to handle encoding the foreign characters:

<?php
include 'data.php'; // contains component data similar to above.

foreach ($component_data as $id => $value) {
  foreach ($value as $language => $translation) {
    $value[$language] = str_replace(
      array("<",">"),
      array('<','>'), 
      htmlentities($translation, ENT_NOQUOTES, "UTF-8")
      );
  }
  echo "UPDATE `components` SET `component_content`='".mysql_real_escape_string(serialize($value))."' WHERE `id` = '$id';\n";
}

?>

The important bit was the ENT_NOQUOTES which meant a simple str_replace could deal with open and closing tags (no maths in the text thankfully), and the mysql_real_escape_string could handle the single quotes. Glad that's over.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文