如何在不使用strlen()的情况下在php中查找字符串长度

如何在不使用strlen()的情况下在php中查找字符串长度,php,c,Php,C,如何在不使用strlen()的情况下在php中查找字符串长度?对于包含这些字母表的单词,条件是取a=b=c=2的值?看起来像是面试官问过你。。。您可以使用mb_strlen() <?php echo mb_strlen("Hello World"); 解决方案是使用。不管怎样,strlen()对于Unicode字符串是无效的。它可能会帮助您。。。未经测试 $s = 'string'; $i=0; while ($s[$i] != '') { $i++;

如何在不使用strlen()的情况下在php中查找字符串长度?对于包含这些字母表的单词,条件是取a=b=c=2的值?

看起来像是面试官问过你。。。您可以使用mb_strlen()

<?php
echo mb_strlen("Hello World");

解决方案是使用。不管怎样,strlen()对于Unicode字符串是无效的。

它可能会帮助您。。。未经测试

   $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;
}
回显获取字符串长度('aaaaa');//将回显5


<?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");
?>

让我问你strlen()有什么问题?
?如果有内置函数,那么你为什么要使编程复杂化。?可能重复不喝水你怎么不口渴?我知道使用strlen很容易,但我不能使用它,因为这是我的任务。所以没有答案对你是正确的???请输入@(@$string{$i}!='')然后更正输出。不,那是错误的。我在那里添加了
&
this1需要@,事实上我没有得到令人满意的答案,为什么放@this1该函数在工作,没有理由让那里
@
我检查了它…是的,如果字符串长度>1000000呢?这并不困难,你可以删除并扩展这个rinfinte,如果你想的话..我以这个为例,你将如何在
for
循环中写无限?你可以使用while-lopp for-infinte,或者在for-loop中不放任何东西来进行infinte循环???在我的答案中有
while
循环…那么我的答案对你来说是正确的。。。
<?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");
?>