douqiu0351 2018-10-24 10:14
浏览 304
已采纳

Node.js等效于php中的htmlspecialchars

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

  • 写回答

1条回答

  • doudaochu1699 2018-10-24 10:18
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?