0

I am using ent package for Encode and decode HTML entities in one of my nodejs project. An example conversion is given below

var ent          = require('ent'); 
let encodeString = ent.encode( 'Lorem Ipsum  Dolor 542  O’ipsum for ❤️ dummy' ) ;
encodeString give result as  Lorem Ipsum 🚲 Dolor 542  O’ipsum for ❤️ dummy

But I cannot reproduce the same result when I tried in php, Code is give below

$string = addslashes(trim(htmlentities('Lorem Ipsum  Dolor 542  O’ipsum for ❤️ dummy', ENT_COMPAT, "UTF-8")))
Result:   Lorem Ipsum  Dolor 542  O’ipsum for ❤️ dummy
Expected: Lorem Ipsum 🚲 Dolor 542  O’ipsum for ❤️ dummy 

Someone please help me to achive the same result in php same as the one obtained from nodejs ent.encode

Note: DB collation as utf8mb4_unicode_ci and saving without encode doesn't work for some emoji's. I found that If I could save date in php as Lorem Ipsum 🚲 Dolor 542 O’ipsum for ❤️ dummy It will work correctly.

Ajith
  • 2,476
  • 2
  • 17
  • 38
  • 1
    What particular problem does this difference make? – Your Common Sense Jul 07 '23 at 09:54
  • @YourCommonSense If the data is save as `Lorem Ipsum 🚲 Dolor 542 O’ipsum for ❤️ dummy` in my mysql database, Then only I can decode it correctly on both php and nodejs front end's . SO I am expecting a php encode solution equivalent to ent.encode function – Ajith Jul 07 '23 at 09:59
  • 1
    1. Use `utf8mb4` charset for both tables and connection. 2. DO NOT use any html encoding for database interactions – Your Common Sense Jul 07 '23 at 10:01
  • @YourCommonSense I tried that also, But the cycle symbol doesn't work , Conclusion I found is If I could save date in php as `Lorem Ipsum 🚲 Dolor 542 O’ipsum for ❤️ dummy` It will work correctly. – Ajith Jul 07 '23 at 10:08
  • 1
    Well you "tried" it wrong. You must use utf8mb4 charset for both tables and connection – Your Common Sense Jul 07 '23 at 10:13
  • @YourCommonSense Thanks it works when I use `utf8mb4` in connections But is there a solution in php ? That will be good as there is already encoded data exists in the database and the db is large – Ajith Jul 07 '23 at 10:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254396/discussion-between-ajith-and-your-common-sense). – Ajith Jul 07 '23 at 10:28
  • 1
    You don't need to encode or decode emojis, consider them as characters and display them as they are. – A.L Jul 07 '23 at 11:11

0 Answers0