469,332 Members | 6,821 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

strlen coming up with wrong value

KeredDrahcir
426 256MB
I;m trying to get the length of a string and then convert it into an array of characters. I need to know the length of the string that can be different lengths but when I use strlen it comes up with 57 when the string is 55 characters.

Expand|Select|Wrap|Line Numbers
  1. //This fuction is almost the same as the fuction str_split included in php5.
  2.   function strsplt($thetext,$num)
  3.   {
  4.     if (!$num)
  5.     {
  6.       $num=1;
  7.     }
  8.     $arr=array();
  9.     $xfive=floor(strlen($thetext)/$num);
  10.     while ($ifive<=$xfive)
  11.     {
  12.       $yfive=substr($thetext,$jfive,$num);
  13.       if ($yfive)
  14.       {
  15.         array_push($arr,$yfive);
  16.       }
  17.       $ifive++;
  18.       $jfive=$jfive+$num;
  19.     }
  20.     return $arr;
  21.   }
  22.  
  23.   $code=$_GET['code'];
  24.   if ($code)
  25.   {
  26.     $size=strlen($code);
  27.     $decode=strsplt($code, 1);
I did a var_dump and was told the array had 55 elements in it, where as strlen told me it was 57 characters long.
Am I doing wrong here?
Dec 16 '10 #1
8 3430
code green
1,726 Expert 1GB
You've probably got whitespace - carriage rturn newline etc
Dec 16 '10 #2
KeredDrahcir
426 256MB
I haven't. Does it count zeros?
Dec 16 '10 #3
JKing
1,206 Expert 1GB
It will count any character numeric or alpha. Do you have an example of the string you are using?
Dec 16 '10 #4
KeredDrahcir
426 256MB
code=D65sydf7foz3wz8sneomrnvy4chcpyrze3j36pzb3q4re 07p3mzjwm10D

If I change the first 5 to a 0 or one of the zero to other numbers strlen gives me a different value.
Dec 16 '10 #5
JKing
1,206 Expert 1GB
I can't replicate that issue at all. strlen() gives me 63 with the string provided. If I replace all the characters with zeros it still gives me 63.
Dec 16 '10 #6
KeredDrahcir
426 256MB
Could there be a problem with my strsplt function then? I had to use that since I don't have php 5.
That should create an array containing everything right of the equal sign.
Dec 16 '10 #7
JKing
1,206 Expert 1GB
It is possible. You don't set the initial values for $ifive or $jfive which could be causing problems. Also if $ifive is supposed to start at 0 you would want your while loop to exit when ($ifive<$xfive)
Dec 16 '10 #8
johny10151981
1,059 1GB
i cannot believe that strlen is giving you wrong count..

The zero you are talking about: if you replace "5" with "0" it will be "0" who's ASCII value is 0x30. if you want to replace 5 with ASCII value 0 then you will have to replace with "\0". In this case you will lost all data after "\0". In C if you enter a '\0' and then use strlen to count it will count until '\0' but yet you can read all the data even after '\0' but in case PHP you will loose all the data after '\0' for sure. if you want to separate all character in a array then

Expand|Select|Wrap|Line Numbers
  1. $string = "this is string"; 
  2.  
  3. $nr = 0; 
  4.  
  5. while (isset($string{$nr})) { 
  6.     $data[$nr] = $string{$nr}; 
  7.     $nr++;   
  8. }  
  9.  
you can follow the example i got on some site. now cant recall the address
Dec 17 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Stephan | last post: by
7 posts views Thread by stephan querengaesser | last post: by
1 post views Thread by colleen1980 | last post: by
reply views Thread by =?Utf-8?B?QW5pcnVkZGhh?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.