简体   繁体   English

使用$ str [0]获取字符串的第一个字符

[英]Getting the first character of a string with $str[0]

I want to get the first letter of a string and I've noticed that $str[0] works great. 我想获取字符串的第一个字母,我注意到$str[0]很好用。 I am just not sure whether this is 'good practice', as that notation is generally used with arrays. 我只是不确定这是否是“好的做法”,因为该符号通常用于数组。 This feature doesn't seem to be very well documented so I'm turning to you guys to tell me if it's all right – in all respects – to use this notation? 这个功能似乎没有得到很好的记录,因此我想请大家告诉我在所有方面都可以使用此符号吗?

Or should I just stick to the good ol' substr($str, 0, 1) ? 还是我应该坚持使用优良的substr($str, 0, 1)

Also, I noted that curly braces ( $str{0} ) works as well. 另外,我注意到花括号( $str{0} )也可以工作。 What's up with that? 那是怎么回事?

Yes. 是。 Strings can be seen as character arrays, and the way to access a position of an array is to use the [] operator. 字符串可以看作是字符数组,访问数组位置的方法是使用[]运算符。 Usually there's no problem at all in using $str[0] (and I'm pretty sure is much faster than the substr() method). 通常,使用$str[0]根本没有问题(而且我敢肯定它比substr()方法快得多)。

There is only one caveat with both methods: they will get the first byte , rather than the first character . 两种方法都只有一个警告:它们将获得第一个字节 ,而不是第一个字符 This is important if you're using multibyte encodings (such as UTF-8). 如果您使用多字节编码(例如UTF-8),则这一点很重要。 If you want to support that, use mb_substr() . 如果要支持该功能,请使用mb_substr() Arguably, you should always assume multibyte input these days, so this is the best option, but it will be slightly slower. 可以争论的是,这些天您应该始终假定输入多字节,因此这是最佳选择,但速度会稍慢。

The {} syntax is deprecated as of PHP 5.3.0. {}语法自PHP 5.3.0起已弃用。 Square brackets are recommended. 建议使用方括号。

Lets say you just want the first char from a part of $_POST, lets call it 'type'. 假设您只希望$ _POST的一部分中的第一个字符,将其称为“类型”。 And that $_POST['type'] is currently 'Control'. $ _POST ['type']当前为“控件”。 If in this case if you use $_POST['type'][0] , or substr($_POST['type'], 0, 1) you will get C back. 如果在这种情况下,如果您使用$_POST['type'][0]substr($_POST['type'], 0, 1)您将获得C

However, if the client side were to modify the data they send you, from type to type[] for example, and then send 'Control' and 'Test' as the data for this array, $_POST['type'][0] will now return Control rather than C whereas substr($_POST['type'], 0, 1) will simply just fail. 但是,如果客户端要修改发送给您的数据(例如,从typetype[] ,然后发送“ Control”和“ Test”作为该数组的数据,则$_POST['type'][0]现在将返回Control而不是Csubstr($_POST['type'], 0, 1)只会失败。

So yes, there may be a problem with using $str[0] , but that depends on the surrounding circumstance. 因此,是的,使用$str[0]可能会出现问题,但这取决于周围的情况。

My only doubt would be how applicable this technique would be on multi-byte strings, but if that's not a consideration, then I suspect you're covered. 我唯一的疑问是该技术在多字节字符串上的适用性,但是如果不考虑这一点,那么我怀疑您已被覆盖。 (If in doubt, mb_substr() seems an obviously safe choice.) (如果有疑问, mb_substr()似乎是一个安全的选择。)

However, from a big picture perspective, I have to wonder how often you need to access the 'n'th character in a string for this to be a key consideration. 但是,从全局角度来看,我想知道您需要多长时间访问一次字符串中的第n个字符才能将此作为关键考虑因素。

It'll vary depending on resources, but you could run the script bellow and see for yourself ;) 它会因资源而异,但是您可以在下面运行脚本并亲自查看;)

<?php
$tests = 100000;

for ($i = 0; $i < $tests; $i++)
{
    $string = md5(rand());
    $position = rand(0, 31);

    $start1 = microtime(true);
    $char1 = $string[$position];
    $end1 = microtime(true);
    $time1[$i] = $end1 - $start1;

    $start2 = microtime(true);
    $char2 = substr($string, $position, 1);
    $end2 = microtime(true);
    $time2[$i] = $end2 - $start2;

    $start3 = microtime(true);
    $char3 = $string{$position};
    $end3 = microtime(true);
    $time3[$i] = $end3 - $start3;
}

$avg1 = array_sum($time1) / $tests;
echo 'the average float microtime using "array[]" is '. $avg1 . PHP_EOL;

$avg2 = array_sum($time2) / $tests;
echo 'the average float microtime using "substr()" is '. $avg2 . PHP_EOL;

$avg3 = array_sum($time3) / $tests;
echo 'the average float microtime using "array{}" is '. $avg3 . PHP_EOL;
?>

Some reference numbers (on an old CoreDuo machine) 一些参考数字(在旧的CoreDuo机器上)

$ php 1.php 
the average float microtime using "array[]" is 1.914701461792E-6
the average float microtime using "substr()" is 2.2536706924438E-6
the average float microtime using "array{}" is 1.821768283844E-6

$ php 1.php 
the average float microtime using "array[]" is 1.7251944541931E-6
the average float microtime using "substr()" is 2.0931363105774E-6
the average float microtime using "array{}" is 1.7225742340088E-6

$ php 1.php 
the average float microtime using "array[]" is 1.7293763160706E-6
the average float microtime using "substr()" is 2.1037721633911E-6
the average float microtime using "array{}" is 1.7249774932861E-6

It seems that using the [] or {} operators is more or less the same. 似乎使用[]{}运算符大致相同。

Speaking as a mere mortal, I would stick with $str[0] . 简而言之,我会坚持使用$str[0] As far as I'm concerned, it's quicker to grasp the meaning of $str[0] at a glance than substr($str, 0, 1) . 就我而言,一目了然地掌握$str[0]的含义要比substr($str, 0, 1)更快。 This probably boils down to a matter of preference. 这可能归结为偏好问题。

As far as performance goes, well, profile profile profile. 就性能而言,概要概要文件概要文件。 :) Or you could peer into the PHP source code... :)或者您可以查看PHP源代码...

$str = 'abcdef';
echo $str[0];                 // a

In case of multibyte (unicode) strings using str[0] can cause a trouble. 如果使用多字节(unicode)字符串,则使用str[0]可能会造成麻烦。 mb_substr() is a better solution. mb_substr()是更好的解决方案。 For example: 例如:

$first_char = mb_substr($title, 0, 1);

Some details here: Get first character of UTF-8 string 这里有一些细节: 获取UTF-8字符串的第一个字符

I've used that notation before as well, with no ill side effects and no misunderstandings. 我以前也使用过这种表示法,没有不良的副作用,也没有误解。 It makes sense -- a string is just an array of characters, after all. 这是有道理的-毕竟,字符串只是字符数组。

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

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