如何在不使用 strlen() 的情况下在 php 中查找字符串长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5989221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find string length in php with out using strlen()?
提问by mymotherland
How can you find the length of a string in php with out using strlen()
?
如何在不使用的情况下在 php 中找到字符串的长度strlen()
?
回答by Vaibhav Jain
I know this is a pretty old issue, but this piece of code worked for me.
我知道这是一个很老的问题,但是这段代码对我有用。
$s = 'string';
$i=0;
while ($s[$i] != '') {
$i++;
}
print $i;
回答by Narayan
$inputstring="abcd";
$tmp = ''; $i = 0;
while (isset($inputstring[$i])){
$tmp .= $inputstring[$i];
$i++;
}
echo $i; //final string count
echo $tmp; // Read string
while - Iterate the string character 1
by 1
而-迭代字符串的字符1
由1
$i
- gives the final count of string.
$i
- 给出字符串的最终计数。
isset($inputstring[$i])
- check character exist(null) or not.
isset($inputstring[$i])
- 检查字符是否存在(空)。
回答by Craig White
Lets be silly
让我们傻
function stupidCheck($string)
{
$count = 0;
for($i=0; $i<66000; $i++)
{
if(@$string[$i] != "")$count++;
else break;
}
return $count;
}
回答by Spudley
I guess there's the mb_strlen()
function.
我猜有这个mb_strlen()
功能。
It's not strlen()
, but it does the same job (with the added bonus of working with extended character sets).
它不是strlen()
,但它做同样的工作(还有使用扩展字符集的额外好处)。
If you really want to keep away from anything even related to strlen()
, I guess you could do something like this:
如果你真的想远离任何与 相关的东西strlen()
,我想你可以这样做:
$length = count(str_split($string));
I'm sure there's plenty of other ways to do it too. The real question is.... uh, why?
我相信还有很多其他方法可以做到这一点。真正的问题是……呃,为什么?
回答by aagjalpankaj
The answer given by Vaibhav Jain will throw the following notice on the last index.
Vaibhav Jain 给出的答案将在最后一个索引上抛出以下通知。
Notice: Uninitialized string offset
注意:未初始化的字符串偏移量
I would like to give an alternative for this:
我想为此提供一个替代方案:
<?php
$str = 'India';
$i = 0;
while(@$str[$i]) {
$i++;
}
echo 'Length of ' . $str . ' is ' . $i . '.';
回答by Krish R
mb_strlen
— Get string length
mb_strlen
— 获取字符串长度
$string ='test strlen check';
print 'mb_strlen(): ' . mb_strlen( $string, 'utf8' ) . "\n\n";
synatx: int mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )
语法: int mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )
str- The string being checked for length.
str- 被检查长度的字符串。
encoding- The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used.
encoding- encoding 参数是字符编码。如果省略,将使用内部字符编码值。
回答by Alex Westergaard
It's going to be a heavy work for your server, but a way to handle it.
这对您的服务器来说将是一项繁重的工作,但却是一种处理它的方法。
function($string){
$c=0;
while(true){
if(!empty($string[$c])){
$c=$c+1;
} else {
break; // Prevent errors with return.
}
}
return $c;
}
回答by RKM
$str = "STRING";
$i=0; $count = 0;
while((isset($str{$i}) && $str{$i} != "")){
$i++; $count++;
}
print $count;
回答by David T. Sadler
This doesn't use loops but may face the O(n) issue if strrpos uses strlen internally.
这不使用循环,但如果 strrpos 在内部使用 strlen 可能会面临 O(n) 问题。
function length($str)
{
return strrpos($str.'_', '_', -1);
}
回答by ale5000
function my_strlen($str)
{
for($i=-1; isset($str[++$i]); );
return $i;
}
echo my_strlen("q");