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

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入