2017-06-30 44 views
1

在外部网站上使用Wordpress供稿并遇到包含字符(如单引号和短划线)的帖子标题问题。将unicode转换回文本 - Wordpress JSON api

JSON输出看起来是这样的:

{"rendered":"Some title post text here – some text after dash"} 

{"rendered":"‘Title text in quotes ’: More title text"} 

什么是这些Unicode字符转换回文本在前端的最佳方式?目前它正在显示unicode。

思考必须有一个JavaScript解决方案。理想的解决方案将涵盖所有基地。

+0

你有没有解决的办法? –

+0

我有,但问题的一部分是知道要搜索什么!我认为这一定是一个常见问题,但在网上找不到太多内容。 – okass

+0

这似乎是一个有用的资源? http://ourcodeworld.com/articles/read/188/encode-and-decode-html-entities-using-pure-javascript – okass

回答

0

一个(排序哈希)方式是使用浏览器对unescape字符串进行操作:可以将原始字符串插入元素(如HTML)并回读文本值。

使用jQuery的帮助:

var result = $("<div/>").html("&#8216;Title text in quotes &#8217;: Dash: &#8211; ").text(); 

result则包含转义纯文本。

看到这个工作小提琴:http://jsfiddle.net/ppbd58jk/

0

它们实际上是HTML实体,你可以将它们添加到一个HTML元素,然后像这样再次阅读。

function html_entity_decode(message) { 
 
    var element = document.createElement("div"); 
 
    element.innerHTML = message; 
 
    return element.innerHTML; 
 
} 
 
console.log(html_entity_decode("Some title post text here &#8211; some text after dash")); 
 
console.log(html_entity_decode("&#8216;Title text in quotes &#8217;: More title text"));