简体   繁体   English

Node.js 相当于 php 中的 htmlspecialchars

[英]Node.js Equivalent for htmlspecialchars in php

i'm actually getting data from my mysql database in which there is text with accents like我实际上是从我的 mysql 数据库中获取数据,其中有带有重音的文本,例如

" é è à â ê... " “é à à ê...”

but when i render it to my ejs file with node.js it shows me但是当我使用 node.js 将它渲染到我的 ejs 文件时,它会显示给我

"h lo" for "hélo" "h lo" 代表 "hélo"

. . The problem is easyli solver with php and the function htmlspecialchars(str);问题是带有 php 和htmlspecialchars(str);函数的 easyli 求解器htmlspecialchars(str); is there any equivalent for this function in Node ? Node 中是否有此功能的等效项?

Thank you谢谢

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.首先,如果您正确设置了编码(在 html 页面和数据库中或在插入之前/选择之后进行转换),则不需要将特殊字符转换为 HTML 实体以避免所描述的行为。 htmlspecialchars() should be used to prevent people from posting HTML (converting < and > to &lt; and &gt; ). htmlspecialchars()应该用于防止人们发布 HTML(将<>转换为&lt;&gt; )。

If you still need to escape HTML special characters, take a look at this NPM package : https://www.npmjs.com/package/html-entities如果您仍然需要转义 HTML 特殊字符,请查看此 NPM 包: 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM