简体   繁体   English

在PHP中引用多字节字符串的最简单方法是什么?

[英]What is the simplest way to reference a Multibyte string in PHP?

My question: In PHP it is easy to reference the single byte string, $single = "abc"; 我的问题:在PHP中,引用单字节字符串$single = "abc";很容易$single = "abc"; for example, 例如,

echo $single[0]; //"a"

However for the multibyte string $multi = "äåö" , I get "nonsense", that is, 但是对于多字节字符串$multi = "äåö" ,我得到的是“废话”,即

echo $multi[0]; //?

I know one can reference the individual letters of a multi byte string by coding as follows: 我知道可以通过编码如下来引用多字节字符串的各个字母:

mb_internal_encoding("UTF-8");
echo mb_substr($multi,1,1);//which gives the right answer "å"

But isn't there an easier way to do this? 但是,难道没有更简单的方法吗? I am especially looking for a way where I can reference the multi byte string with square brackets and just one parameter as for the single byte case. 我特别在寻找一种方法,可以引用带有方括号的多字节字符串,而对于单字节大小写则仅引用一个参数。

No, Unicode support is on the table for new versions, but it is not ready yet. 不,新版本已经在支持Unicode,但尚未准备好。 It will get there at some point presumably, but not yet. 它大概会在某个时候到达那里,但还没有。

from the manual : 从手册

A string is series of characters, where a character is the same as a byte. 字符串是一系列字符,其中一个字符与一个字节相同。 This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. 这意味着PHP仅支持256个字符的集合,因此不提供本机Unicode支持。

The closest you could get to this is implementing an object implementing ArrayAccess using a __toString() function and a LOT of fiddling. ArrayAccess最接近的是使用__toString()函数和很多ArrayAccess实现一个实现ArrayAccess的对象。 Not recommended in my opinion. 我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM