What are some best practices for handling smiley replacements in PHP scripts?

When handling smiley replacements in PHP scripts, it is important to properly sanitize and validate user input to prevent any potential security vulnerabilities such as XSS attacks. One best practice is to use a predefined list of smiley symbols and their corresponding HTML entities to ensure safe substitution.

// Define an array mapping smiley symbols to their corresponding HTML entities
$smileyMap = array(
    ':)' => '🙂',
    ':D' => '😀',
    // Add more smiley symbols and their HTML entities as needed
);

// Function to replace smiley symbols with their corresponding HTML entities
function replaceSmileys($text) {
    global $smileyMap;
    return strtr($text, $smileyMap);
}

// Example usage
$inputText = "Hello there! :)";
$outputText = replaceSmileys($inputText);
echo $outputText;