gpt4 book ai didi

php - 为什么使用 iconv_strpos 而不是 strpos?

转载 作者:行者123 更新时间:2023-12-04 00:39:53 24 4
gpt4 key购买 nike

In contrast to strpos(), the return value of iconv_strpos() is the number of characters that appear before the needle, rather than the offset in bytes to the position where the needle has been found. (Source: http://www.php.net/manual/en/function.iconv-strpos.php)

以下代码示例显示 iconv_strpos()strpos() 返回相同的值。

$string = "dd.MM.yy";

echo "d: ".strpos($string, 'd'); // 0
echo "M: ".strpos($string, 'M'); // 3
echo "y: ".strpos($string, 'y'); // 6

echo "d: ".iconv_strpos($string, 'd'); // 0
echo "M: ".iconv_strpos($string, 'M'); // 3
echo "y: ".iconv_strpos($string, 'y'); // 6

为什么我应该使用 iconv_strpos 而不是 strpos?

最佳答案

通常只有在使用多字节编码(如 UTF-8 或 UTF-16)时才相关。

一个字符可能由多个字节组成(UTF-8 中的非 7 位 ASCII 字符就是这种情况——它们的编码长度是可变的。UTF-16 有 2 个字节的字符)。

关于php - 为什么使用 iconv_strpos 而不是 strpos?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5500052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com