The principle of operation of my code is as follows: if there are such lowercase emoticons (":)", ":(") in the entered line, then you need to replace them with pictures. Here is how I do it:

$smile = array(":)", ":("); $grafic = array("<img src = './image/Smile.png' alt='Smile' align='middle'>", "<img src = './image/Sad.png' alt='Smile' align='middle'>"); $new_message = str_replace($smile, $grafic, $message); $file = "../data/messages.json"; $json_content = json_decode(file_get_contents($file), true); if (!empty($new_message)) { $json_content[] = array("time" => $time, "user" => $user, "message" => $new_message); file_put_contents($file, json_encode($json_content, JSON_PRETTY_PRINT)); } 

But then, the already changed line I need to write to the "base" ( json file) turns out the following:

 [ { "time": "1499985376", "user": "Max", "message": "Hello <img src = '.\/image\/Smile.png' alt='Smile' align='middle'>" } ] 

How can I make it so that, for example, the word "Smile" or "Sad" is written instead of HTML code?

  • 3
    And why do you convert text smiles into pictures before writing to the database? Convert them when withdrawing from the database! - Visman
  • one
    And if the message was the word "Sad", then after saving and restoring it will become a smiley? That makes me a 😞🐼. - Nick Volynkin
  • Many messengers have a setting: show emoticons as they are or replace them with emoji. Obviously, they transmit and store text, and the replacement is made at the display stage. This also allows you to change the display of already saved text. And if you immediately replaced upon receipt :) on a piece of HTML, then it will be difficult to change the picture. Therefore, I support the point of view of @Visman - convert only after withdrawal from the database. - Nick Volynkin

1 answer 1

The strip_tags function removes html and php tags from a string.

Example:

 echo strip_tags("<p>Какой-то текст</p>"); 

Output result:

Some text

Documentation link