Solved

jQuery Html Friendly Characters

Posted on 2009-07-15
7
737 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
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…

778 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