不同服务器上的字符集不同?

发布于 2024-08-07 10:29:09 字数 2092 浏览 6 评论 0 原文

我刚刚在本地测试了我的网络应用程序,一切正常,但上传到服务器应用程序后表现不同。我使用函数 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

但在本地,我没有这个问题

I've just tested locally my web application, everything works fine, but after uploading to server application behaves differently. I use function formatiraj_string_url to convert diacritic symbols and get clean url... locally it works fine but on server this function doesnt convert them the same way.

Few days earlier I tested this on some third server and it worked fine. Now I'm uploading web to test it again on this third server, but I just wonder what could really be the cause of such behavior?

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;
    }

EDIT: Before str_replace, this function used preg_replace. On server this was the error showed:

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

But locally, I didn't have this problem

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

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

发布评论

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

评论(2

我不吻晚风 2024-08-14 10:29:09

我建议您依靠现有的、经过严格测试的代码来执行此操作。我相信所有这些函数都假设 UTF-8 输入和输出 7 位 ASCII:

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:

醉生梦死 2024-08-14 10:29:09

您的文件是用哪个字符集编写的?

由于您已将要替换的字符作为字符串直接写入代码中,因此它们会以文件使用的任何字符集进行编码。如果该字符集与您从浏览器获得的字符集不同,您的功能将无法工作。

您需要记住的重要一点是始终跟踪编码字符串的字符集,并在必要时进行转换。

查看 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.

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