?
Solved

jQuery Html Friendly Characters

Posted on 2009-07-15
7
Medium Priority
?
742 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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

765 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