473,734 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strlen coming up with wrong value

426 Contributor
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.   }
  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 3680
code green
1,726 Recognized Expert Top Contributor
You've probably got whitespace - carriage rturn newline etc
Dec 16 '10 #2
426 Contributor
I haven't. Does it count zeros?
Dec 16 '10 #3
1,206 Recognized Expert Top Contributor
It will count any character numeric or alpha. Do you have an example of the string you are using?
Dec 16 '10 #4
426 Contributor
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.
Dec 16 '10 #5
1,206 Recognized Expert Top Contributor
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
426 Contributor
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
1,206 Recognized Expert Top Contributor
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
1,059 Top Contributor
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"; 
  3. $nr = 0; 
  5. while (isset($string{$nr})) { 
  6.     $data[$nr] = $string{$nr}; 
  7.     $nr++;   
  8. }  
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

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 is written as follows: <i><input type="checkbox" name="chk_Complete" value="<%Response.Write l_IsChecked%>"<%if cbool(l_IsChecked) then Response.Write " checked"%>> The code to captures the checkbox value in the asp page that builds the sql string is follows
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: wofstream outdata("test.tmp"); wstring s; long l1=256; long l2=250; wchar_t c1=(wchar_t)l1; wchar_t c2=(wchar_t)l2; s+=l1;
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, but RecordsAffected reports 0 instead of 1. Here is the code... -------- Dim dbMyData As Database Dim strSQL As String
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 returns a value which is one lower than the Item I want to edit, so when I update the record, the previous record is changed. I put some code here, but not all. Adding, of course, is no problem because then the index is set to -1. I tried to use the DataKeyField property in my controls, but that...
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 method, i have to pass a new instance of the filter-object without setting any properties. but the value type-properties can´t be null and the filter is set to 0 (int) or false (bool). therefore i did implement the propertySpecified-pattern like this:
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 You.
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 out of another row. The field that is equal for the 2 lines is ItemCode Original Row The Fields that make the row which holds the good value unique is ItemCode and Country (Country= FR) Wrong row
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 node. How can I remove it? Any pointer would be very helpful.
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(); testDataTable.Columns.Add("updatedDateTime", typeof(DateTime)); DataRow testDateRow=surveyUpdatedDateDataTable.NewRow();
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 the first letter of a a particular field, and it will use that first letter in the criteria of the query to include only records with a value beginning with that letter. The tricky part is that one of the possible values for that field is - *.* - not values I would have chosen, but they are...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.