Text registration with emoticons

Asked

Viewed 552 times

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 Moticon, this Emoticon can also be saved in the database and displayed later. Someone has some solution to help me?

  • What will Emoticon represent as a reserved word? e.g.: [smile] [joinha] ... because if so, you can treat these reserved words when showing the image in the news reading

  • No, exactly why... may be different "symbols" that the user insert

  • What you’ve tried, and what went wrong?

3 answers

1

For that you have to keep your comic book 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 you just act normal like it’s plain text.

You can read more about it here:http://andy-carter.com/blog/saving-emoticons-unicode-from-twitter-to-a-mysql-database

1

Using preg_replace you can map the emoticons and replace them with their corresponding image. Inserting the image directly or 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 classes smiles_01 and smiles_02 with 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>
  • I was able to save directly in the database, changing all the coding of the database, tables and columns to utf8mb4.

-1

You can save more or less like this:

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

Ai at the time of saving display in HTML will already be a little guy. Got?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.