简体   繁体   中英

PHP - strlen returning incorrect string length caused by quote

I have the following string coming from a database: Let's Get Functional

If I run this through strlen it returns 25 characters instead of the expected 20. A var dump shows that the string looks like the above (no html references etc).

If I remove the single quote strlen returns 19 characters.

Obviously the quote is returning 5 characters instead of 1 - why? How can I stop this?

Thanks!

The ASCII entity name is ' for ' , that equals to 5 chars : ASCII Table

The problem seems to be related to the fact that your ' is evaluated, not just readed. Try to get your strings like that :

$myString = 'Hello World!';

Not like this :

$myString = "Hello World!";

Which reads and evaluates all your string's content instead of just reading, and so interpreting your special chars as their ASCII code.

PHP Manual says : If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters

I think your strlen() function is called with parameters containing " , so it gives the evaluated strlen() , not the readed.

try this :

$countChars = strlen(utf8_decode($myString));

utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.

Take a look at this for more informations about differences between simple and double quotes.

It can not be.

<?php
$str = "Let's Get Functional";
echo strlen($str), "\n"; // 20

Look at code output here.

how to debug?

print the ASCII code of each char:

$str = "Let's Get Functional";
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
    echo "$i\t", ord($str[$i]), "\n";
}

this is the result:

0   L       76
1   e       101
2   t       116
3   '       39
4   s       115
5           32
6   G       71
7   e       101
8   t       116
9           32
10  F       70
11  u       117
12  n       110
13  c       99
14  t       116
15  i       105
16  o       111
17  n       110
18  a       97
19  l       108

As @deformhead already explained, it seems that your apostrophe has been converted to the HTML &apos; string. My guess would be that between getting the string out of the database and calling strlen() on it you call htmlentities() somewhere in-between.

You can also check how many characters you get from the database in your select query with CHAR_LENGTH() (MySQL).

Another issue you might consider is that strlen() does not work well for multibyte characters so if you'll be working with non-ASCII characters then you'd better use mb_strlen() with the correct encoding. This case however would not explain the difference of 5 characters in your result (strlen() counts the bytes and not characters in a string).

Hope that helps.

<?php

$string = "Let's Get Functional";

echo strlen($string);

?>

This code returns 20 characters.

I have had same problem as you , may be this will help someone.

The single quote was converted to "& #39;" which was giving me incorrect result. simply replacing the string with single quote have solved my problem.

$string = "Let's Get Functional"; //string from POST or Database

echo strlen($string); //25

$string = str_replace("'", "'",$string); echo strlen($string); //20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM