在mysql中序列化php存储多语言数据
我必须将一些数据插入 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) unserialize
d 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 unserializ
able state...
... and for bonus points, if you can convert the utf8 foreign language chars to HTML entitities and from markdown into HTML
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试删除“mysql_real_escape_string”以查看反序列化是否有效?
您可以尝试的另一件事是对序列化数组进行 Base64 编码。
然后对其进行 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.
And then base64_decode it and unserialise when you retrieve it.
感谢大家提出的有用建议。
实际上,问题出在存储的texture(不是markdown!)和多语言utf-8的混合上。将其压缩到 MySQL 中的解决方案有点粗糙。首先对数据集运行 Textile 以将其转换为 html,然后通过以下内容对其进行 munge,以处理外来字符的编码:
重要的一点是
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:
The important bit was the
ENT_NOQUOTES
which meant a simplestr_replace
could deal with open and closing tags (no maths in the text thankfully), and themysql_real_escape_string
could handle the single quotes. Glad that's over.