Solved

jQuery Html Friendly Characters

Posted on 2009-07-15
7
735 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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 …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now