I'm trying to find the actual length of a string returned by filter_var() function using strlen() and mb_str() functions.
Code:
<?php
$name = "Carl O'Johnson";
echo "The actual length of the string: " . strlen($name) . "<br>";
$name = filter_var($name, FILTER_SANITIZE_STRING);
echo "The length of the string using strlen() after filter_var() function: " . strlen($name) . "<br>";
echo "The length of the string using mb_strlen() after filter_var() function: " . mb_strlen($name) . "<br>";
?>
Output:
The actual length of the string: 14 The length of the string using strlen() after filter_var() function: 18 The length of the string using mb_strlen() after filter_var() function: 18
The filter_var function encodes ' as #39;. Which function will return the actual length of the string returned by filter_var() function?
You either want to leave the quotes alone when sanitizing the string (if that's fine with you), by setting the FILTER_FLAG_NO_ENCODE_QUOTES
option:
$name = filter_var($name, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
$length = mb_strlen($name); // 14
Or you could transform the generated HTML entities back before computing the length, making use of html_entity_decode
and passing the ENT_QUOTES
flag:
$name = filter_var($name, FILTER_SANITIZE_STRING);
$length = mb_strlen(html_entity_decode($name, ENT_QUOTES, 'UTF-8')); // 14
Note that in this case, strlen
would produce the same output.