Replacing characters with images

How do I replace character :)emoji with images <img src="smiles/aa.gif" alt=":)">?


Answer 1, authority 100%

The function str_replace– searches for a value in the variable that was passed to it and changes it to the one that was given in another argument…

An article about it from the official documentation πŸ™‚ :

str_replace– Replaces the search string with a replacement string

Description
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int $&count ] )

This function returns a subjectstring or array with all occurrences of searchreplaced by replace. If complex find/replace rules are not needed, using this function is preferable to ereg_replace()or preg_replace().

As of PHP 4.0.5, any str_replace()argument can be an array.

Attention
In versions prior to 4.3.3, this function has a bug when passing arrays in the searchand replacearguments at the same time. The error is that empty elements of the searcharray were skipped without moving to the next element of the replacearray. This bug has been fixed in PHP 4.3.3. If your scripts used this error, then they need to remove empty elements from the search array before calling this function.

If subjectis an array, the search and replace is performed on each element of that array, and the array is also returned.

If both searchand replaceare arrays, then str_replace()uses all the values ​​of the array searchand corresponding replacearray values ​​to search and replace in subject. If the replacearray has fewer elements than the searcharray, the empty string will be used as the replacement string for the remaining values. If searchis an array and replaceis a string, then replacewill be used as the replacement string for each element of the searcharray .

Examples of using str_replace()

<?php
//  <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// : Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// : You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
//    5.0.0   count
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2
?>

Answer 2

You can indeed use str_replace.
But, there is one but. As I understand it, your messages are placed in the database (although I may be wrong). In this case, it is probably more logical to change your text emoticons to graphic ones not at the moment of entering information into the database, but to replace it at the moment of data sampling from the database and displaying them on the page.