PHP では、以下が有効です。
$n='abc';
echo $n[1];
しかし、以下はそうで'abc'[1];
はないようです。
そのパーサーに何か問題がありますか?
残念ながら、現在のところ、$n[1]
構文でさえあまり有用ではありません ◕︵◕ 。これは、Unicode をサポートしておらず、文字ではなくバイトを返すためです。
echo 'abc'[1];
PHP 5.5
完全な RFC でのみ有効ですが$n[1] or $n{2}
、すべてのバージョンで有効ですPHP
残念ながら、現在のところ、
$n[1]
構文でさえあまり有用ではありません ◕︵◕ 。これは、Unicode をサポートしておらず、文字ではなくバイトを返すためです。
あなただけのものを作成してみませんか?? 例 :
$str = "Büyük";
echo $str[1], PHP_EOL;
$s = new StringArray($str);
echo $s[1], PHP_EOL;
// or
echo new StringArray($str, 1, 1), PHP_EOL;
出力
�
ü
ü
クラス使用
class StringArray implements ArrayAccess {
private $slice = array();
public function __construct($str, $start = null, $length = null) {
$this->slice = preg_split("//u", $str, - 1, PREG_SPLIT_NO_EMPTY);
$this->slice = array_slice($this->slice, (int) $start, (int) $length ? : count($this->slice));
}
public function slice($start = null, $length = null) {
$this->slice = array_slice($this->string, (int) $start, (int) $length);
return $this ;
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->slice[] = $value;
} else {
$this->slice[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->slice[$offset]);
}
public function offsetUnset($offset) {
unset($this->slice[$offset]);
}
public function offsetGet($offset) {
return isset($this->slice[$offset]) ? $this->slice[$offset] : null;
}
function __toString() {
return implode($this->slice);
}
}
いいえ、これは適切な操作です。あなたがしようとしていることをするために、あなたは試すことができます:
echo substr('abc', 1, 1);
リテラル文字列アクセス構文'abc'[1]
は、JavaScript では非常に有効ですが、PHP ではバージョン5.5までサポートされません。