How to find string length in php without using strlen()?the condition is to take value of a=b=c=2, for words containing these alphabets?
5 Answers
You can use mb_strlen(), it will process Unicode strings.
Or use this function:
function get_string_length($string) {
$i = 0;
while ($string{$i} != '') {
$i++;
}
return $i;
}
echo get_string_length('aaaaa'); // will echo 5
8 Comments
Imaan
put @ in while (@$string{$i} != '') then correct output.
Legionar
No, thats wrong. I added there
&Imaan
this1 needs @ ,infact i am not getting satisfactory answer that why put@ this
Legionar
That function is working, and there is no reason to have there
@Imaan
i checked it ... u r right , happy
|
<?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");
?>
6 Comments
Legionar
And what if string length is > 1000000??
Imaan
this is not difficult, u can remove and extend this r infinte , if u want.. i took this as an example
Legionar
how will you write infinite in
for loop??Imaan
you can use while lopp for infinite purpose or put nothing in for loop for infinte looping
Legionar
??? in my answer there is
while loop... So then my answer is correct for you... |
strlen().?If there is a built in function then why are you complicating the programming.?