将波兰角色存储为PHP中的变量

发布于 2025-02-09 02:24:21 字数 311 浏览 2 评论 0原文

我正在创建一个简单的注册表格,其中您必须将昵称输入到Textarea中。当用户使用波兰字符(Ś,,等)时,就会发生问题。当我尝试通过回声显示整个字符串时,它看起来应该如何,但是当我尝试仅显示一个字符时,它就会显示此怪异的符号。

function ech($nam)
{
    echo $nam;
    echo "</br>";
    echo $nam[1];
}

$te = $_POST['sth']; //$te equals "śżć" now
ech($te);

输出:

ść

I am creating a simple registration form, in which you must type your nickname into textarea. The problem occurs when user uses polish characters (ś,ż,ć etc.). When I try to display the whole string by echo it looks just how it should, but when I try to display only one character then it shows this weird � symbol.

function ech($nam)
{
    echo $nam;
    echo "</br>";
    echo $nam[1];
}

$te = $_POST['sth']; //$te equals "śżć" now
ech($te);

Output:

śżć

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

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

发布评论

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

评论(1

回忆凄美了谁 2025-02-16 02:24:21

使用偏移量为字符串的字符,例如$ nam [1]实际上仅返回单个字节,但字符是多个字节。使用多级安全 - 安全字符串函数,例如mb_substr($ nam,0,1)

  • php字符串是字节数组(与例如JavaScript相比,它们是UTF16字符阵列),在UTF-8中字符串“Ś”包含2个字节,执行strlen(“Ś”)给您2个字节,做bin2hex(“ st”)给您“ c59b”,当您进行$ str [0]时,您只是在获取第一个字节组成的2个字节本身就毫无意义,因此您可以在做$ str [0]时得到�(fwiw做echo $ str [0]。$ str [1]也可以使用为2个字节,您会手动获取前两个字节)

Using an offset for a character of a string like $nam[1] actually only returns a single byte, but the characters are multiple bytes. use multibyte-safe string functions like mb_substr($nam, 0, 1)

  • php strings are byte arrays (in contrast to, for example, JavaScript, where they are utf16 character arrays), in UTF-8 the string "ś" contains 2 bytes, doing strlen("ś") gives you 2, doing bin2hex("ś") gives you "c59b", and when you do $str[0] you are only fetching the first byte of the 2 bytes that makes up ś, which on it's own happens to mean nothing, hence you get the � when doing $str[0] (fwiw doing echo $str[0].$str[1] would also work because ś happens to be 2 bytes and you'd manually fetch the first 2 bytes)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文