What potential pitfalls should be avoided when trying to write code to calculate the length of meta descriptions in PHP?

When calculating the length of meta descriptions in PHP, it's important to avoid pitfalls such as not properly handling multibyte characters or HTML entities, which can affect the actual displayed length. To accurately calculate the length, you should first decode any HTML entities and then use mb_strlen() function to handle multibyte characters.

function calculateMetaDescriptionLength($metaDescription) {
    $decodedDescription = html_entity_decode($metaDescription);
    $length = mb_strlen($decodedDescription);
    
    return $length;
}

// Example of how to use the function
$metaDescription = "This is a meta description with special characters & emojis 😊";
$descriptionLength = calculateMetaDescriptionLength($metaDescription);
echo "Meta Description Length: " . $descriptionLength;