简体   繁体   English

如何将特殊字符转换为html?

[英]How to convert special characters to html?

I currently have this: 我目前有这个:

var loc = "Stati Uniti d'America"
jQuery('#heading_results h2').text(loc);

The result i got is: 我得到的结果是:

<h2>Stati Uniti d&#039;America</h2>

This is because for other reasons, I am sending via php the string with special characters but i need to convert them back once in the html. 这是因为由于其他原因,我通过php发送带有特殊字符的字符串,但是我需要在html中将它们转换回一次。

It should come out as 它应该作为

<h2>Stati Uniti d'America</h2>

In order to use & character codes, you must use .html() : 为了使用&字符代码,必须使用.html()

 var loc = "Stati Uniti d&#039;America" jQuery('#heading_results h2').html(loc); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="heading_results"> <h2></h2> </div> 

string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] ) 字符串htmlspecialchars(字符串$ string [,int $ flags = ENT_COMPAT | ENT_HTML401 [,字符串$ encoding = ini_get(“ default_charset”)[,bool $ double_encode = true]]])

enter link description here 在此处输入链接说明

Rather than using jQuery and dummy html element you can use plain function to convert your text: 您可以使用普通函数来转换文本,而不是使用jQuery和虚拟html元素:

 var str = "Stati Uniti d&#039;America"; function decode(text) { return text.replace(/&#([0-9A-Z]{2,3});/g, function (match, numStr) { var num = parseInt(numStr, 10); return String.fromCharCode(num); }); } console.info(decode(str)); 

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

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