不同服务器上的字符集不同?
我刚刚在本地测试了我的网络应用程序,一切正常,但上传到服务器应用程序后表现不同。我使用函数 formatiraj_string_url 来转换变音符号并获取干净的 url...在本地它工作正常,但在服务器上此函数不会以相同的方式转换它们。
几天前,我在第三台服务器上测试了这个,效果很好。现在我正在上传网页以在第三台服务器上再次测试它,但我只是想知道这种行为的真正原因是什么?
function formatiraj_string_url($string)
{
$string = strtolower($string);
$znak[0] = ' ';
$znak[1] = 'Š';
$znak[2] = 'š';
$znak[3] = 'Đ';
$znak[4] = 'đ';
$znak[5] = 'Č';
$znak[6] = 'č';
$znak[7] = 'Ć';
$znak[8] = 'ć';
$znak[9] = 'Ž';
$znak[10] = 'ž';
$znak[11] = 'Š';
$znak[12] = 'Đ';
$znak[13] = 'Č';
$znak[14] = 'Ć';
$znak[15] = 'Ž';
$znak[16] = 'š';
$znak[17] = 'đ';
$znak[18] = 'č';
$znak[19] = 'ć';
$znak[20] = 'ž';
$znak[21] = 'Š'; // Š
$znak[22] = 'š'; // š
$zamjena[0] = '-';
$zamjena[1] = 's';
$zamjena[2] = 's';
$zamjena[3] = 'd';
$zamjena[4] = 'd';
$zamjena[5] = 'c';
$zamjena[6] = 'c';
$zamjena[7] = 'c';
$zamjena[8] = 'c';
$zamjena[9] = 'z';
$zamjena[10] = 'z';
$zamjena[11] = 's';
$zamjena[12] = 'd';
$zamjena[13] = 'c';
$zamjena[14] = 'c';
$zamjena[15] = 'z';
$zamjena[16] = 's';
$zamjena[17] = 'd';
$zamjena[18] = 'c';
$zamjena[19] = 'c';
$zamjena[20] = 'z';
$zamjena[21] = 's';
$zamjena[22] = 's';
$string = str_replace($znak, $zamjena, $string);
$new_string = preg_replace("/[^a-zA-Z0-9-s]/", "", $string);
return $new_string;
}
编辑:在 str_replace 之前,该函数使用 preg_replace。在服务器上显示错误:
Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in /home2/sinjcom/public_html/sinj.com.hr/administracija/include/funkcije.php on line 200
但在本地,我没有这个问题
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您依靠现有的、经过严格测试的代码来执行此操作。我相信所有这些函数都假设 UTF-8 输入和输出 7 位 ASCII:
remove_accents()
来自 Wordpressutf8_to_ascii()
来自古老的 PHP UTF8 项目Friendly_url()
作者:Jakub VránaDoctrine_Inflector::urlize()
slugify()
看起来很可靠I recommend you rely on existing, highly-tested code to do this. I believe all these functions assume UTF-8 input and output 7-bit ASCII:
remove_accents()
from Wordpressutf8_to_ascii()
from the venerable PHP UTF8 projectfriendly_url()
by Jakub VránaDoctrine_Inflector::urlize()
slugify()
looks solid您的文件是用哪个字符集编写的?
由于您已将要替换的字符作为字符串直接写入代码中,因此它们会以文件使用的任何字符集进行编码。如果该字符集与您从浏览器获得的字符集不同,您的功能将无法工作。
您需要记住的重要一点是始终跟踪编码字符串的字符集,并在必要时进行转换。
查看 Kore Nordmanns 有关 PHP 字符集的常见问题解答 有关字符集的更多指示。
Which charset is your file written in?
Since you have written the characters you would like to replace directly into your code, as strings, they are encoded in whatever charset the file uses. If that charset differs from what you get from the browser, your functions aren't going to work.
The important point you need to keep in mind, is to always keep track of the charset your strings are encoded with, and convert when neccessary.
Take a look at Kore Nordmanns FAQ about charsets in PHP for some more pointers about charsets.