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;
}
Here's the output,
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.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
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