Solved

jQuery Html Friendly Characters

Posted on 2009-07-15
7
739 Views
Last Modified: 2012-05-07
in jQuery I was wondering if there is a function that will convert the following string into
html friendly characters

var myString="@£¥®©®";

but using jquery it will convert that string to the following

@£¥®©®

So basically any Symbol in a string would get converted into the HTML friendly characters

Here is the chart below

http://www.ascii.cl/htmlcodes.htm
0
Comment
Question by:stargateatlantis
  • 3
  • 2
  • 2
7 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 300 total points
ID: 24862154
>>in jQuery
no. try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script  type="text/javascript">
<!--
function start(){
	alert( friendly("a@b.com") )
}
 
function friendly(str)
{
	var searchFor="&,@,£,¥,®,©";
	var replaceWith="amp,pound,yen,reg,copy";
	
	searchFor=searchFor.split(",");
	replaceWith=replaceWith.split(",");
	
	var i=-1,limit=searchFor.length;
	while(++i<limit)
	{
		str=str.replace( new RegExp(searchFor[i],"g"), "&"+replaceWith[i]+";" );
	}
 
return str;
}
window.onload=start;
//-->
</script>
 
</body>
</html>

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 24862353
But it would be great if you can replace any non numeric or alpha characters with html friendly
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 300 total points
ID: 24862572
Not all of them have a "friendly" name, but all have a numeric code. Not sure why you want the friendly name, when the numeric entities will achive the same thing for a wider number of characters. IF you truly want the friendly names option then update the previous post:
        var searchFor="&,@,£,¥,®,©";
        var replaceWith="amp,pound,yen,reg,copy";

with the characters you want to replace. (BTW: remove @ from the searchFor list). Just to clarify, if you choose to go with the numeric codes, then £ would be converted to &#163; instead ot &pound;. If you were to work with xml, then &pound; would NOT be recognized automatically. You would need to declare it first. However, numeric entities work without having to take extra steps.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 18

Assisted Solution

by:bruno
bruno earned 200 total points
ID: 24862889
try this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script  type="text/javascript">
<!--
function start(){
        alert( convert("a®b.com") )
}
 
 
function convert(str) {
	var selectedCharacters = str.split("");
	var transformedString = new Array();
 
	// loop over the characters in selected text
	for (i=0;i<selectedCharacters.length;i++) {
		// if ascii value of current character is greater than 127, then transform
		if (selectedCharacters[i].charCodeAt(0) > 127) {
			transformedString.push("&#"+selectedCharacters[i].charCodeAt(0)+";");
		} 
		// if ascii value of current character is less than 127, then leave as is
		else {
			transformedString.push(selectedCharacters[i]);
		}
 
	}
 
	// replace selected text with special-characters-translated text
	newStr = transformedString.join("");
	return newStr;
}
 
 
window.onload=start;
//-->
</script>
 
</body>
</html>

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 24863378
Can this be used with jQuery?
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 300 total points
ID: 24863456
>>But it would be great if you can replace any non numeric or alpha characters with html friendly
If I understand you correctly, you want Letters and Numbers to be left alone, but encode everything else. If so, use:
function numeric(str)
{
	var i=-1,limit=str.length;
	var result="";
	while(++i<limit)
	{
		if( /[0-9a-zA-Z]/.test( str.charAt(i) ) )
		{
			result+=str.charAt(i)
		}
		else
		{
			result+= "&#"+str.charCodeAt(i)+";"
		}
	}
return result;
}

Open in new window

0
 
LVL 18

Assisted Solution

by:bruno
bruno earned 200 total points
ID: 24864456
any javascript can be used with jquery. don't get hung up on jquery.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Diminish Pop-up  in 3 seconds 7 44
jQuery Scroll To Top 5 37
ajax to record click 3 15
HowTo set a global variable in javascript or jQuery 4 21
This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question