strlen coming up with wrong value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KeredDrahcir
    Contributor
    • Nov 2009
    • 426

    #1

    strlen coming up with wrong value

    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.

    Code:
    //This fuction is almost the same as the fuction str_split included in php5.
      function strsplt($thetext,$num)
      {
        if (!$num)
        {
          $num=1;
        }
        $arr=array();
        $xfive=floor(strlen($thetext)/$num);
        while ($ifive<=$xfive)
        {
          $yfive=substr($thetext,$jfive,$num);
          if ($yfive)
          {
            array_push($arr,$yfive);
          }
          $ifive++;
          $jfive=$jfive+$num;
        }
        return $arr;
      }
    
      $code=$_GET['code'];
      if ($code)
      {
        $size=strlen($code);
        $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?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    You've probably got whitespace - carriage rturn newline etc

    Comment

    • KeredDrahcir
      Contributor
      • Nov 2009
      • 426

      #3
      I haven't. Does it count zeros?

      Comment

      • JKing
        Recognized Expert Top Contributor
        • Jun 2007
        • 1206

        #4
        It will count any character numeric or alpha. Do you have an example of the string you are using?

        Comment

        • KeredDrahcir
          Contributor
          • Nov 2009
          • 426

          #5
          code=D65sydf7fo z3wz8sneomrnvy4 chcpyrze3j36pzb 3q4re07p3mzjwm1 0D

          If I change the first 5 to a 0 or one of the zero to other numbers strlen gives me a different value.

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            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.

            Comment

            • KeredDrahcir
              Contributor
              • Nov 2009
              • 426

              #7
              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.

              Comment

              • JKing
                Recognized Expert Top Contributor
                • Jun 2007
                • 1206

                #8
                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)

                Comment

                • johny10151981
                  Top Contributor
                  • Jan 2010
                  • 1059

                  #9
                  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

                  Code:
                  $string = "this is string"; 
                  
                  $nr = 0; 
                  
                  while (isset($string{$nr})) { 
                      $data[$nr] = $string{$nr}; 
                      $nr++;   
                  }
                  you can follow the example i got on some site. now cant recall the address

                  Comment

                  Working...