如何在不使用 php 的情况下查找字符串长度strlen()
条件是取值 a=b=c=2,对于包含这些字母的单词?
好像面试官问了你...好吧,您可以使用mb_strlen()
<?php
echo mb_strlen("Hello World");
(或)
使用这个..在SO之前的某个地方阅读它
<?php
echo array_sum(count_chars("Hello World"));
它可能会帮助你...未测试...
$s = 'string';
$i=0;
while ($s[$i] != '') {
$i++;
}
print $i;
你可以
使用 mb_strlen()
,它将处理 Unicode 字符串。
或者使用此功能:
function get_string_length($string) {
$i = 0;
while ($string{$i} != '') {
$i++;
}
return $i;
}
echo get_string_length('aaaaa');//将回显 5
解决方案是使用 mb_strlen()。 无论如何,strlen() 对于 Unicode 字符串来说是被破坏的。
<?php
function mystrlen($str) {
$count = 0;
for ($i = 0; $i < 1000000; $i++) {
if (@$str[$i] != "") {
if (@$str[$i] == "a" || @$str[$i] == "b" || @$str[$i] == "c") {
$count+=2;
} else {
$count++;
}
}
else {
break;
}
}
return $count;
}
echo mystrlen("this is temporary but we made it complex");
?>