How can PHP be used to handle and display emoticons or smileys in a form input field?

To handle and display emoticons or smileys in a form input field using PHP, you can use a combination of HTML and PHP. You can create a mapping of emoticons to their corresponding image URLs and then replace the emoticons in the input field with the corresponding image tags using PHP.

<?php
// Mapping of emoticons to image URLs
$emoticons = array(
    ':)' => 'smile.png',
    ':(' => 'sad.png',
    ':D' => 'laugh.png'
);

// Function to replace emoticons with image tags
function replaceEmoticons($text) {
    global $emoticons;
    foreach ($emoticons as $emoticon => $image) {
        $text = str_replace($emoticon, '<img src="' . $image . '" alt="' . $emoticon . '">', $text);
    }
    return $text;
}

// Sample input text with emoticons
$inputText = 'Hello :) How are you?';

// Display input text with emoticons replaced by images
echo replaceEmoticons($inputText);
?>