472,342 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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 3605
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

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

Similar topics

4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code...
2
by: jackie | last post by:
Here is my prolem: i try to output wstring with unicode value greater than 255(for each wchar_t) to a file such as test.tmp. Here is my code: ...
2
by: Jack | last post by:
It appears that RecordsAffected reports the wrong value when a Null integer or date value is inserted using empty quotes. The insert works fine,...
3
by: Stephan | last post by:
Hi, I'm working on a page with multiple Datalist-controls on it. The problem I'm facing is that when I try to update a record, the ItemIndex...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i donīt want to filter the return value of the...
1
by: colleen1980 | last post by:
Hi: I try both of them but when i check in debugger both of them shows the wrong value. k = Combo2.Value k = Forms!frmDHG!Combo2.Value Thank...
1
by: Plieka | last post by:
I'm strugling to create a query for the following problem I have a SQL2000 Database and I would like to update 1 field out of 1 row with the value...
0
by: =?Utf-8?B?QW5pcnVkZGhh?= | last post by:
Hi, I am trying to build one web application using Visual Studio 2005. I am using menu control. But one Arrow sign is coming with the main menu...
6
by: Manikandan | last post by:
Hi, I need to insert the datetime with milliseconds value into a datarow. My code as below DataTable testDataTable=new DataTable(); ...
2
by: Coll | last post by:
I'm working with a form & a query that allows the user to filter the data using several combo boxes. One of the combo boxes allows them to select...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.