[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

jQuery Html Friendly Characters

Posted on 2009-07-15
7
Medium Priority
?
744 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1200 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 1200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Assisted Solution

by:bruno
bruno earned 800 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 1200 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 800 total points
ID: 24864456
any javascript can be used with jquery. don't get hung up on jquery.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

649 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