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 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?