PHP strlen() Not Working Correctly??

PHP strlen() Not Working Correctly Because Single or Double Quotes - How to Get Correct strlen() PHP

Home Short:

If you have a string and the function strlen() in PHP seems to be returning a str length greater than it actually is, does your string have either single and/or double quotes ', " ??.

The reason is most likely because you previously called a string cleaning function to sanitize the string, ie htmlentities($str, ENT_QUOTES, 'UTF-8') which converted single quotes ' in the string to ' or ' and double quotes " were converted to " or ".

A suggested solution is to use the html_entity_decode() which may or may NOT work.

The solution is to manually replace these html entities yourself. The code below uses 2 small functions to do this for you.

The first function simplifies replace multiple items in a string at the same time and the second function then uses this function to properly convert the string so you get an accurate return on the built in PHP strlen() function.

S
H
A
R
E