可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to get the first letter of a string and I've noticed that $str[0]
works great. 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)
?
Also, I noted that curly braces ($str{0}
) works as well. What's up with that?
回答1:
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).
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). If you want to support that, use mb_substr()
. Arguably, you should always assume multibyte input these days, so this is the best option, but it will be slightly slower.
回答2:
The {} syntax is deprecated as of PHP 5.3.0. Square brackets are recommended.
回答3:
Lets say you just want the first char from a part of $_POST, lets call it 'type'. And that $_POST['type'] is currently 'Control'. If in this case if you use $_POST['type'][0]
, or substr($_POST['type'], 0, 1)
you will get C
back.
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.
So yes, there may be a problem with using $str[0]
, but that depends on the surrounding circumstance.
回答4:
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.)
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.
回答5:
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)
$ 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.
回答6:
Speaking as a mere mortal, I would stick with $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)
. 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...
回答7:
$str = 'abcdef';
echo $str[0]; // a
回答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.
回答9:
In case of multibyte (unicode) strings using str[0]
can cause a trouble. mb_substr()
is a better solution. For example:
$first_char = mb_substr($title, 0, 1);
Some details here: Get first character of UTF-8 string
回答10:
Consider the following:
<?php
var_dump($_POST);
// Post var is a string:
echo '1'; var_dump( strtoupper( $_POST['flag1'][0] ) === 'T' ); // boolean true
echo '2'; var_dump( strtoupper( substr( $_POST['flag1'],0,1 ) ) === 'T' ); // boolean true
// Post var is an array of strings
echo '5'; var_dump( strtoupper( $_POST['flag2'][0] ) === 'T' ); // boolean false
echo '6'; var_dump( strtoupper( substr( $_POST['flag2'],0,1 ) ) === 'T' ); // generates a PHP warning
?>
<hr />
<form method="POST">
<input type="text" name="name" value="bob" /><br />
<input type="text" name="flag1" value="true" /><br />
<select name="flag2[]" multiple>
<option value="true" selected>true</option>
<option value="false" selected>false</option>
<option value="0" selected>0</option>
</select><br />
<input type="submit" value="Submit" /><br />
</form>
The intention is to detect a boolean flag. Although it is possible to confuse the issue by turning the var into an array type, using the array-subscript notation produces a safe false result, whereas the substr() function produces an error. Given the choice here, I'd go with the subscript notation.
回答11:
This is how I had solved the problem:
<?php
$string = 'Hello The World';
$stringExp = explode(' ', $string);
$shortCode = '';
for($i = 0; $i < count($stringExp); $i++):
$shortCode .= substr($stringExp[$i], 0, 1);
endfor;
echo $shortCode; // result : HTW
?>
回答12:
It's easy to get the first character of a string. Just treat the string as an array.
Example:
$first = $string{0};
$fifth = $string{5};
That's it!