Problem with strlen() and wrong char counts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jimburns
Forum Newbie
Posts: 8
Joined: Sun Jun 07, 2009 11:30 am

Problem with strlen() and wrong char counts

Post by jimburns »

Need some help with a problem with strlen() not reporting the length of a string correctly. I've been scratching my head on this for a day now and there has got to be some esoteric gottcha behind all this that I just can't see.

Simply, I have a logging class that writes activity to a log file. The user of the logging class simply passes the logging class Write method a string. The logging class needs to do some formatting on this string and handle some wrapping so naturally I'm using the strlen() to investigate how to write the passed string. But the passed string ends up being 2 or 3 or so chars longer than it really is.

Here's a synopsis of what I'm doing.

User function: Rather than making multiple logging calls, the external function assembles it's log message as it proceeds.

//-------------------------------------------------------------------

Code: Select all

 
function USER() {
  ...
  $logMsg = 'RICheckAlbums:: Checking tbl_picalbums for $yearmonth='.$yearmonthcode;
  ...
  ...
  $logMsg .= '  Existing entry FOUND!  pid = '.$result; 
  ...
 
  $uniquelogMsg = $logMsg;                     // was trying to reallocate string
 
  echo 'len. logMsg='.strlen($logMsg).H_CR;
  echo 'len. uniquelogMsg='.strlen($uniquelogMsg).H_CR;
  echo 'len. literal='.strlen('RICheckAlbums:: Checking tbl_picalbums for $yearmonth=200902 Existing entry FOUND! pid = 2').H_CR;
 
  echo 'LOGGING Msg'.H_CR;
  $aLog->Write($uniquelogMsg); 
}
 
//-------------------------------------------------------------------
function CLOG->Write($basemessage) {
 
    echo '$bm = |'.$basemessage.PIPE.H_CR;
 
    $k = strlen(trim($basemessage));
    $myheadache = $basemessage;
    echo '$k='.$k.H_CR;
    echo 'mbstrlen='.mb_strlen($basemessage, 'ASCII').H_CR;
    echo '$mha='.strlen($myheadache).H_CR;
 
}
 
Now when the Write function echos the passed message, I'm purposely surrounding it with the '|' char so I can definitively see the string in the output. H_CR is a '<br />' define.

Here's the output,
len. logMsg=92
len. uniquelogMsg=92
len. literal=90
LOGGING Msg
$bm = |RICheckAlbums:: Checking tbl_picalbums for $yearmonth=200902 Existing entry FOUND! pid = 2|
$k=92
mbstrlen=92
$mha=92
As you can see, both copies of my intended string, $logMsg and $uniquelogMsg are reported as 92 before being sent to the CLog-Write() method. A check on the actual length of the string I'm sending via strlen() and the literal string reports 90. This IS the correct length I expect.

Once inside the Write() method, the parameter $basemessage is output with surrounding PIPES for visibility and some lengths are calculated with strlen(). They all report 92, 3 chars more than the expected. If you count the chars between the PIPES in the output you'll find they equal 90.

What am I missing. I need a good length to process this string but I'm not seeing why the length is not reported correctly. Also, notice it has something to do with the string concatenation. I originally thought this was something internal to the Write() method, maybe something in the pass. So I added the echos in the USER function and after the concatenation operation the string's length is no longer accurate. IF I comment out the addition of the second string (ie. the first concatenation) in the USER function, all reported lengths are correct.

Help?

TIA,

/jimburns
Last edited by Benjamin on Sun Jun 07, 2009 11:31 pm, edited 1 time in total.
Reason: Added [code=php] and [quote] tags.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with strlen() and wrong char counts

Post by requinix »

Here's what you typed for the original message:

Code: Select all

$logMsg = 'RICheckAlbums:: Checking tbl_picalbums for $yearmonth='.$yearmonthcode;
$logMsg .= '  Existing entry FOUND!  pid = '.$result;
I count 92 characters.

For the copy:

Code: Select all

RICheckAlbums:: Checking tbl_picalbums for $yearmonth=200902 Existing entry FOUND! pid = 2
I count 90.

See the problem?
jimburns
Forum Newbie
Posts: 8
Joined: Sun Jun 07, 2009 11:30 am

Re: Problem with strlen() and wrong char counts

Post by jimburns »

See the problem? No, hence my post.

You state you count 92 characters in your first quoted block but I don't know how you count that string as the variables are not resolved. It resolves in fact to exactly your second string in appearance but strlen() still reports 92.

The issue still remains unresolved. In fact, in every case, no matter how many concatenations I may do to the original string, it's always 2 off. Certainly this should be telling but I just don't see what the concatenation does to add the two extra.

/j
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Problem with strlen() and wrong char counts

Post by Darhazer »

In the first quote there are 2 more spaces than in the second. Hence the difference of the lenghts
jimburns
Forum Newbie
Posts: 8
Joined: Sun Jun 07, 2009 11:30 am

Re: Problem with strlen() and wrong char counts

Post by jimburns »

Look again at the output
len. logMsg=92
len. uniquelogMsg=92
len. literal=90
LOGGING Msg
$bm = |RICheckAlbums:: Checking tbl_picalbums for $yearmonth=200902 Existing entry FOUND! pid = 2|
$k=92
mbstrlen=92
$mha=92
$bm is the PASSED string from USER. Being displayed in the browser I was concerned with redundant spaces being removed at the browser. I echoed the $bm line with the string surrounded by PIPES to aid in seeing the string's extent itself. Yet I can't see any unaccounted for spaces.

strlen($bm) still reports 92, yet you see it there in the output, only 90.

Still wondering...

/j
Last edited by Benjamin on Sun Jun 07, 2009 11:32 pm, edited 1 time in total.
Reason: Added [quote] tags.
jimburns
Forum Newbie
Posts: 8
Joined: Sun Jun 07, 2009 11:30 am

Re: Problem with strlen() and wrong char counts

Post by jimburns »

Ahh... jeez.... alright... I see it...
jimburns
Forum Newbie
Posts: 8
Joined: Sun Jun 07, 2009 11:30 am

Re: Problem with strlen() and wrong char counts

Post by jimburns »

Thanks for the extra eyes.... It looks like this was a case of using the browser and not figuring on it's removal of runs of spaces....

got it....

Case closed.

thanks,

/j
Post Reply