Leon Gaban
Leon Gaban

Reputation: 39018

How to unescape Unicode character in ActionScript 3 AS3

I'm having a problem with "&".

Basically I haven't been able to escape this correctly in Flash AS3, however I did find this link which seems helpful: http://www.smithmediafusion.com/blog/?p=343

Test page: http://touchstormdigital.com/leon/testing/

Here are my current functions:

try {
    varHome   = this.loaderInfo.parameters.home;
    homeImage = this.loaderInfo.parameters.homeImage;
    homeTitle = this.loaderInfo.parameters.homeText; // Get the Video Title
    // Title  = "Quick & easy chicken recipes for dinner"
} catch (e:Error) {
    varHome   = "false";
    homeBool  = false;
}

The Search and Replace function:

// Home player test search & replace ampersand
private function replaceString(str:String, find:String, replace:String):String 
{
    var startIndex:Number = 0;
    var oldIndex:Number   = 0;
    var newString:String  = "";

    while ((startIndex = str.indexOf(find, startIndex)) != -1)
    {
        newString += str.substring(oldIndex, startIndex) + replace;
        oldIndex = startIndex += find.length;
    }

    return((newString == "") ? str : newString);
}

And then how I'm using it

private function drawSplash():void
{
    sp = new ScreenButton();

    replaceString(homeTitle,"&", "\\u0026");

    sp.drawScreenButton(playerW, playerH, homeBool, homeImage, homeTitle);
    sp.addEventListener("onPlay", vd.playVideo);
    sp.addEventListener("embedSplash", hideSplash);
    stage.addChild(sp);
}

I think this is where the problem is:

replaceString(homeTitle,"&", "\\u0026");

I've also tried this:

replaceString(homeTitle,"&", String.fromCharCode(38));

GOAL Grab "Quick & easy chicken recipes for dinner" and display it

Still displaying just "Quick"

Test page: http://touchstormdigital.com/leon/testing/


UPDATE! Another simple fix found and no need to change title!

In addition to The_asMan's answer which works, but requires the text/copy to be written all weird, I found this simple piece of javascript that does the job:

http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

How I'm using it:

<script type="text/javascript">

    var homeText = "Quick & easy - chicken' recipes for dinner?!=+";
    var fixed = encodeURIComponent(homeText);

</script>

And then in the Flashvars area:

so.addVariable("homeText", fixed);

Then in Flash:

unescape(homeTitle);

Woot!

Upvotes: 0

Views: 5722

Answers (2)

The_asMan
The_asMan

Reputation: 6402

This line here shows me you are loading flash vars.

homeTitle = this.loaderInfo.parameters.homeText;

For example if you have a url like so.

http://somedomain.com?var1=aa&var2=b&var3=c

There are 3 variable being passed to flash var1,var2,var3
When you do

http://somedomain.com?title=Quick & easy chicken recipes for dinner

You are breaking it in 2 spots the first spot is the ampersand that is telling flash there is a new variable to read. the other spot is the spaces, URLs can not have spaces. so the proper way to encode this would be

http://somedomain.com?title=Quick%20%26%20easy%20chicken%20recipes%20for%20dinner

And then of course in flash an unescape() would be perfect.

Sorry I should have noticed this sooner it didn't dawn on me that the data isn't there.

Upvotes: 1

ToddBFisher
ToddBFisher

Reputation: 11590

Try unescape(homeTitle) as escaping it is the opposite way you want to go.

example: trace(unescape("%26")); //will trace out: &

Upvotes: 2

Related Questions