Node.js Equivalent for htmlspecialchars in php

447
October 25, 2018, at 07:20 AM

i'm actually getting data from my mysql database in which there is text with accents like

" é è à â ê... "

but when i render it to my ejs file with node.js it shows me

"h�lo" for "hélo"

. The problem is easyli solver with php and the function htmlspecialchars(str); is there any equivalent for this function in Node ?

Thank you

Answer 1

First of all, if you set your encoding correctly (on the html page and in the database or convert before inserting/after selecting) you shouldn't need to convert special chars to HTML entities to avoid the described behaviour. htmlspecialchars() should be used to prevent people from posting HTML (converting < and > to &lt; and &gt;).

If you still need to escape HTML special characters, take a look at this NPM package : https://www.npmjs.com/package/html-entities

Use it like this :

const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
console.log(entities.encode("éèàâê");

will output

&eacute;&egrave;&agrave;&acirc;&ecirc;
Rent Charter Buses Company
READ ALSO
Fire trigger for all database changes in mysql

Fire trigger for all database changes in mysql

I need a code that fires the trigger when changes are made in database and the changes should store in log table using mysqlIs there possibility to get that kind of code in mysql? i saw code for SQL Server, but i couldn't find same in mysql

112
Server error 5006: the vendor not specified the redirecturl but already specified the url

Server error 5006: the vendor not specified the redirecturl but already specified the url

Server error 5006: Unable to redirect to Vendor's web siteThe Vendor failed to provide a RedirectionURL

134
Mysql temp password consists of characters like &#39;&lt;&#39;

Mysql temp password consists of characters like '<'

So I am using ansible to install MySQL 57 on Centos 7

93
Join two tables with two identical columns

Join two tables with two identical columns

I have two tables: Log and Location, there is no relationship between the twoHowever in each table there are columns X and Y

134