Discussion:
strlen ?
Jim Giner
2013-07-05 17:10:39 UTC
Permalink
Trying to manage line breaks in some output I'm generating and using
strlen to measure the lengths of the strings I'm printing. Discovered
something strange (to me!) in that strlen is returning +1 more than it
should.

The strings are from a query of my database - simple name fields. But
everyone of them is coming back with a length that is one more than I see.

Ex.

Mike Hall comes back as 10, not 9
F.B. comes back as 5, not 4.

I've looked at my data and counted the chars - there is no extra space
at the beginning or end in my table.

Anyone have an explanation?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Meyer
2013-07-05 17:13:20 UTC
Permalink
Post by Jim Giner
Trying to manage line breaks in some output I'm generating and using
strlen to measure the lengths of the strings I'm printing. Discovered
something strange (to me!) in that strlen is returning +1 more than it
should.
The strings are from a query of my database - simple name fields. But
everyone of them is coming back with a length that is one more than I see.
Ex.
Mike Hall comes back as 10, not 9
F.B. comes back as 5, not 4.
I've looked at my data and counted the chars - there is no extra space
at the beginning or end in my table.
Anyone have an explanation?
Does strlen count the line feed?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
shiplu
2013-07-05 17:32:46 UTC
Permalink
Post by Jim Giner
Mike Hall comes back as 10, not 9
F.B. comes back as 5, not 4.
Doesn't work for me.

php > var_dump("Mike Hall", strlen("Mike Hall"));
string(9) "Mike Hall"
int(9)

Try trimming it first and then apply strlen.
--
Shiplu.Mokadd.im
ImgSign.com | A dynamic signature machine
Innovation distinguishes between follower and leader
Jim Giner
2013-07-05 18:33:35 UTC
Permalink
Post by shiplu
Post by Jim Giner
Mike Hall comes back as 10, not 9
F.B. comes back as 5, not 4.
Doesn't work for me.
php > var_dump("Mike Hall", strlen("Mike Hall"));
string(9) "Mike Hall"
int(9)
Try trimming it first and then apply strlen.
Why would I need to trim something that I can already see doesn't have
any trailing or leading characters?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Matijn Woudt
2013-07-05 18:36:47 UTC
Permalink
Post by Jim Giner
Post by Jim Giner
Mike Hall comes back as 10, not 9
Post by Jim Giner
F.B. comes back as 5, not 4.
Doesn't work for me.
php > var_dump("Mike Hall", strlen("Mike Hall"));
string(9) "Mike Hall"
int(9)
Try trimming it first and then apply strlen.
Why would I need to trim something that I can already see doesn't have
any trailing or leading characters?
Because there are characters you can't see?
Matijn Woudt
2013-07-05 18:42:43 UTC
Permalink
I checked them in the db manually. Clicked on the name, selected it, no
extra space highlighted. Cursored through the length of the value - no
extra movements.
That does still not guarantee there are no extra characters. Some
characters are just not visible (NUL, CR, LF, ..)
Post by Jim Giner
Post by Jim Giner
Mike Hall comes back as 10, not 9
Post by Jim Giner
F.B. comes back as 5, not 4.
Doesn't work for me.
php > var_dump("Mike Hall", strlen("Mike Hall"));
string(9) "Mike Hall"
int(9)
Try trimming it first and then apply strlen.
Why would I need to trim something that I can already see doesn't have
any trailing or leading characters?
Because there are characters you can't see?
Jim Giner
2013-07-05 18:50:18 UTC
Permalink
Post by Matijn Woudt
I checked them in the db manually. Clicked on the name, selected it, no
extra space highlighted. Cursored through the length of the value - no
extra movements.
That does still not guarantee there are no extra characters. Some
characters are just not visible (NUL, CR, LF, ..)
Post by Jim Giner
Post by Jim Giner
Mike Hall comes back as 10, not 9
Post by Jim Giner
F.B. comes back as 5, not 4.
Doesn't work for me.
php > var_dump("Mike Hall", strlen("Mike Hall"));
string(9) "Mike Hall"
int(9)
Try trimming it first and then apply strlen.
Why would I need to trim something that I can already see doesn't have
any trailing or leading characters?
Because there are characters you can't see?
And the answer is - yes, there is a LF char at the end of my data in my
whole table.

Now the question is - how the heck did I put that in there? Certainly
not intentionally. The data is captured from a d/e screen I wrote and
it simply grabs the post value and inserts a new record with that value
along with some other values. And I don't see anything concatenating a
LF to my string.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stephen
2013-07-05 19:02:56 UTC
Permalink
Now the question is - how the heck did I put that in there? Certainly
not intentionally. The data is captured from a d/e screen I wrote and
it simply grabs the post value and inserts a new record with that
value along with some other values. And I don't see anything
concatenating a LF to my string.
Is this a browser being used for input? Never assume what a browser will do.

It is good practice to validate and condition data before inserting into
a database.

Consider trimming the data before doing the INSERT.
--
Stephen
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-07-05 19:27:18 UTC
Permalink
Post by Stephen
Now the question is - how the heck did I put that in there? Certainly
not intentionally. The data is captured from a d/e screen I wrote and
it simply grabs the post value and inserts a new record with that
value along with some other values. And I don't see anything
concatenating a LF to my string.
Is this a browser being used for input? Never assume what a browser will do.
It is good practice to validate and condition data before inserting into
a database.
Consider trimming the data before doing the INSERT.
I do validate my data by quoting it but I never expected to have to do a
trim to remove a LF. Especially on an iphone for input, since it's not
easy to enter a LF.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Meyer
2013-07-05 21:48:10 UTC
Permalink
MOTS: never take any input on faith.
Post by Jim Giner
Post by Stephen
Now the question is - how the heck did I put that in there? Certainly
not intentionally. The data is captured from a d/e screen I wrote and
it simply grabs the post value and inserts a new record with that
value along with some other values. And I don't see anything
concatenating a LF to my string.
Is this a browser being used for input? Never assume what a browser will do.
It is good practice to validate and condition data before inserting into
a database.
Consider trimming the data before doing the INSERT.
I do validate my data by quoting it but I never expected to have to do
a trim to remove a LF. Especially on an iphone for input, since it's
not easy to enter a LF.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
2013-07-06 04:22:34 UTC
Permalink
Post by Jim Giner
Post by Stephen
Post by Jim Giner
Now the question is - how the heck did I put that in there?
Certainly
and
Post by Stephen
Post by Jim Giner
it simply grabs the post value and inserts a new record with that
value along with some other values. And I don't see anything
concatenating a LF to my string.
Is this a browser being used for input? Never assume what a browser
will
Post by Stephen
do.
It is good practice to validate and condition data before inserting
into
Post by Stephen
a database.
Consider trimming the data before doing the INSERT.
I do validate my data by quoting it but I never expected to have to do a
trim to remove a LF. Especially on an iphone for input, since it's not
easy to enter a LF.
Quoting the data is not the same thing as validating it.

Thanks,
Ash
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Lester Caine
2013-07-06 06:59:12 UTC
Permalink
And the answer is - yes, there is a LF char at the end of my data in my whole
table.
Now the question is - how the heck did I put that in there? Certainly not
intentionally. The data is captured from a d/e screen I wrote and it simply
grabs the post value and inserts a new record with that value along with some
other values. And I don't see anything concatenating a LF to my string.
The obvious question that comes to mind is 'What OS'? Having seen this sort of
niggle many times I tend to find it relates to something working 'cross-os' a
bit like windows ignoring case in file name and linux then complaining it can't
find a file.

It's probably worth checking the sting length back through your code just to
confirm what added it?
--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-07-06 14:56:10 UTC
Permalink
Post by Lester Caine
And the answer is - yes, there is a LF char at the end of my data in my whole
table.
Now the question is - how the heck did I put that in there? Certainly not
intentionally. The data is captured from a d/e screen I wrote and it simply
grabs the post value and inserts a new record with that value along with some
other values. And I don't see anything concatenating a LF to my string.
The obvious question that comes to mind is 'What OS'? Having seen this
sort of niggle many times I tend to find it relates to something working
'cross-os' a bit like windows ignoring case in file name and linux then
complaining it can't find a file.
It's probably worth checking the sting length back through your code
just to confirm what added it?
The best I can figure is that I did a preload of many of the names from
a csv file. Apparently when I do that it stores the LF at the end of
the csv line. Have to remember that the next time.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Lester Caine
2013-07-06 18:23:47 UTC
Permalink
Post by Lester Caine
It's probably worth checking the sting length back through your code
just to confirm what added it?
The best I can figure is that I did a preload of many of the names from a csv
file. Apparently when I do that it stores the LF at the end of the csv line.
Have to remember that the next time.
AH - I use that to flag the end of line when I pulled the data apart - so it
gets stripped ;)
Then I found fgetcsv() which replaced the manual code and gave just the data as
an array.
--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Caio Tavares
2013-07-11 14:34:59 UTC
Permalink
Try with mb_strlen

see
http://www.php.net/manual/en/function.mb-strlen.php
Post by Jim Giner
Trying to manage line breaks in some output I'm generating and using
strlen to measure the lengths of the strings I'm printing. Discovered
something strange (to me!) in that strlen is returning +1 more than it
should.
The strings are from a query of my database - simple name fields. But
everyone of them is coming back with a length that is one more than I see.
Ex.
Mike Hall comes back as 10, not 9
F.B. comes back as 5, not 4.
I've looked at my data and counted the chars - there is no extra space at
the beginning or end in my table.
Anyone have an explanation?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...