PDA

View Full Version : "&"



diamondnular
03-28-2007, 03:06 PM
Hi all,

I am trying to write a php code which outputs javascript code like this:

var a="b&c"
but finally, the output source code is

var a="b&c"

My question is: Is there a javascript function read a string containing "&" as "&" like function htmlspecialchars_decode in php?

Thanks,

KC.

Bob90
03-28-2007, 03:15 PM
Its not a built in function, but here is one.
http://javascript.crockford.com/remedial.html
See String Methods >> entityify()

:)

Bob90
03-28-2007, 03:21 PM
Ok.

Just add this at the top of your script and call it with

var a = "b & c".deentityify()



String.prototype.deentityify = function ()
{
return this.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">");
}

diamondnular
03-28-2007, 03:29 PM
Hi Bob90,

Thanks. But I think replacing method does not work here, as every "&" will be translated to "&amp;" in html source code:



var a = "b &amp; c".deentityify()

String.prototype.deentityify = function ()
{
return this.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">");
}


becomes:



var a = "b &amp;amp; c".deentityify()

String.prototype.deentityify = function ()
{
return this.replace(/&amp;amp;/g,"&amp;").replace(/&amp;lt;/g,"<").replace(/&amp;gt;/g,">");
}


Am I right?

KC.

Bob90
03-28-2007, 03:44 PM
Check out my second post. Made my own.

Bob90
03-28-2007, 03:46 PM
OK i see now. Well that has to be PHP then as I can't think of any code.

Unless you use unescape() and instead of &amp; you use %26?

But probably not useful.

diamondnular
03-28-2007, 06:41 PM
Hi Bob90,

Noop, it really helps. This is what I finally came up with:



var a = 'b' + unescape('&#37;26') + 'c';


There is no "&" in the code and the code works just fine. Thanks a lot.

KC.