...

View Full Version : Replace Function for double quote(") to single quote (')



owen_yuen
03-25-2005, 05:10 PM
Just want to replace a double quote(") to single quote ('), but it doesn't replace. The value of classnote (a short paragraph) is return from db.

// Split a classnote into array by space
var words=classnote.split(" ")

// Reset classnote = Empty
classnote = '';

// Replace double quote(") to single quote (') through LOOP
for(i=0;i<words.length;i++)
{
classnotenote = classnotenote + words[i].replace('"',"'") + ' ';
}

IdentityCrisis
03-25-2005, 05:22 PM
Try using the escape character in your function. This will treat the ' as an actual quote.



// Split a classnote into array by space
var words=classnote.split(" ")

// Replace double quote(") to single quote (') through LOOP
for(i=0;i<words.length;i++)
{
classnotenote = classnotenote + words[i].replace('"', '\'') + ' ';
}


IC

owen_yuen
03-25-2005, 05:49 PM
No, it still doesn't replace double quote(") to single quote .... ????

IdentityCrisis
03-25-2005, 05:53 PM
what is the output (or result) you're getting from that function?

IC

owen_yuen
03-25-2005, 05:57 PM
Example:

"classnote" value return from database

Function : classnote = It"s is a test. It"s is a test.
Output: classnote = It"s is a test. It"s is a test.


But I am looking for : classnote = It's is a test. It's is a test.

afru
03-25-2005, 06:08 PM
Hi,

User Regular Expression with /g option to replace all double quotations.



<SCRIPT LANGUAGE="JavaScript">
<!--
var mystr='Hi "how are" you';
mystr=mystr.replace(/\"/g,'\'');
alert(mystr);
//-->
</SCRIPT>


By the way, why r u splitting your classnote? You dont need to split it if you just want to replace quotations. Use classnote inplace of mystr in above code.

Have fun :)

Afru.

Willy Duitt
03-25-2005, 06:16 PM
With the little bit of code you have provided, I see no need to be splitting the string at spaces... Besides, nowhere do I see where you join() the split array again... Below is all you should need to convert all instances of double quotes with single quotes...



<script type="text/javascript">
<!--//
function convertQuotes(string){
return string.replace(/"/g,"'");
}

// EXAMPLE USAGE:
document.write(convertQuotes('It"s is a test. It"s is a test.'));
//-->
</script>


However, why were the single quotes ever converted to double quotes in the first place?? I can understand that the single quotes would conflict with the serverside delimiters but... The single quotes contained within the string sent to the database should have been converted to their ascii equivelent and therefore would not need converting again when returned...

.....Willy

owen_yuen
03-25-2005, 06:20 PM
Thanks

IdentityCrisis
03-25-2005, 06:23 PM
I agree with Willy's comment... why were the single quotes being converted to double quotes anyways? If you're using a database that uses SQL (which is nearly all of them as far as I know, especially for a web site)... you can use the slashes to "escape" the quotes.... example...

'INSERT INTO TABLE (Field) VALUES ('This doesn\'t make sense');

If you're using PHP, it's even easier, there's a function called addslashes that will automatically escape characters that might cause DB hickups.

IC

Willy Duitt
03-25-2005, 07:06 PM
I agree with Willy's comment... why were the single quotes being converted to double quotes anyways? If you're using a database that uses SQL (which is nearly all of them as far as I know, especially for a web site)... you can use the slashes to "escape" the quotes.... example...

'INSERT INTO TABLE (Field) VALUES ('This doesn\'t make sense');

If you're using PHP, it's even easier, there's a function called addslashes that will automatically escape characters that might cause DB hickups.

IC

Using PHP, there are numerous methods to address this problem...


addSlashes()
htmlspecialchars()
htmlentities()


But the bottom line... this needs to be done serverside NOT clientside...
Else, you are going to run across a user with javascript disabled whom will be able to submit the single quotes anyway...

Personally, I would use htmlentities so you do not need to stripSlashes when returned...

.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum