strlen for two or more strings?
Which is the correct way of making strlen return the lenght of several strings put together.
For example if one string is hello and other is jim it should return: 8. (hello=5 jim=3)
I need to get the combine开发者_如何学编程d lenght of $array[0] $array[1] $array[2] $array[3] and $array[4]
Thanks
Use implode on it before:
echo strlen(implode($array));
You can also combine it with array_slice if you don't want the whole array:
echo strlen(implode(array_slice($array, 0, 4)));
array_sum(array_map("strlen", $array_of_strings))
$len = 0;
foreach($array as $str)
$len += strlen($str);
use something like this:
$string = '';
foreach ($array as $val) {
$string .= $val;
}
echo strlen($string);
This will avoid multiple strlen
calls and hence, should be a bit faster then calling strlen
inside the foreach
loop, atleast theoretically.
You can use implode() to transform your array of strings in a single string and then strlen the result.
echo strlen(implode('', $array))
If you want to get the collective length of all of the strings in an array, you can do this:
$len = strlen(implode('',$myArray));
*Note that this code will join the strings together with no spaces between them. You can change the first parameter to implode()
if you want this to be different.
精彩评论