Registration of text with emoticons

1

I have a page where a textarea allows the user to paste news. the content inserted there is saved in a MYSQL database to be displayed on the site page in the future. I need that if the user paste in this textarea a text that contains some emoticon, that emoticon can also be saved in the database and displayed later. Does anyone have a solution to help me?

    
asked by anonymous 25.05.2016 / 02:29

3 answers

1

For this you have to save your DB as utf8mb4

in MySQL:

SET NAMES utf8mb4;
ALTER DATABASE database_name CHARACTER SET = utf8mb4 
COLLATE = utf8mb4_unicode_ci;


ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 
COLLATE utf8mb4_unicode_ci;


ALTER TABLE table_name CHANGE column_name column_name VARCHAR(140)      
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;

Then just do it normally as if it were plain text.

You can read more about this here: link

    
25.05.2016 / 09:52
1

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>
    
25.05.2016 / 11:45
-1

You can save more or less like this:

Um texto qualquer com um <img src="smile/1.png"/> qualquer

At the moment of saving display in the HTML will already be a little face. Got it?

    
25.05.2016 / 09:41