Using preg_replace
you can map emoticons and replace them with their corresponding image. By inserting the image directly or by formatting an element using css
.
Note that the example will return <img "smiles/:].gif">
.
example:
$string = 'Lorem ipsum dolor sit amet, consectetur... :) :]';
$pattern = ['~(\:\))~' , '~(\:\])~'];
$replace = '<img "smiles/${1}.gif">';
echo preg_replace($pattern, $replace, $string);
output:
Lorem ipsum dolor sit amet, consectetur.... <img "smiles/:).gif"> <img "smiles/:].gif">
Using css
, just use the smiles_01
and smiles_02
classes with the image size and background.
example:
$string = 'Lorem ipsum dolor sit amet, consectetur... :) :]';
$pattern = ['~(\:\))~' , '~(\:\])~'];
$replace = ['<div class="smiles_01"></div>' , '<div class="smiles_02"></div>'];
echo preg_replace($pattern, $replace, $string);
output:
Lorem ipsum dolor sit amet, consectetur... <div class="smiles_01"></div> <div class="smiles_02"></div>