dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: String, Character Replace Function, Comments please

  1. #1
    Senior Member
    Join Date
    Aug 2001
    Posts
    227

    String, Character Replace Function, Comments please

    Ok, long story short, i need a way to replace characters throughout a whole string. What i am doing, is loading a big article that is inside an xml attribute (the blah="" tag thing). I am doing various things with the article, such as splitting it up to be in pages, ect.

    My problem lies in the fact that xml hates html tags format.. as they seem to be the same thing basically, so what i am wanting is a way to replace a few key characters that html goes by.

    In otherwords, i need to write stuff like <a href="asfunction:blah,blah">Link</a>, ect in an xml file. But since i dont see a way to do that, i am going to write it with different characters; Something XML is happy with. For example
    Code:
    (,)a href=(')asfunction:blah,blah(')(.)Link(,)/a(.)
    Now i know that looks insanely weird, and i'll prob use dif characters, but what i am doing is replacing anything XML might not like (i cant remember if it doesent like " or not) with my own custom character.

    Now to get to what this post is about, what do you think would be the best way to replace the characters? I may be missing some simple way around this, so please correct me if i am.

    But what i plan on doing, is building a custom replace function with some String features. Say you want to replace all instances of (,) with < after loading the XML Node Attribute into a String. What i am going to do is make my function hold a Variable for completed string, temp string, and old string.

    Apon the function being called, it takes a parameter of a string, searches via String.indexOf, for the first instance of (,). Once it finds an instance you log what number it starts at, lets say it starts at 51 characters in. It then takes characters 0-50, puts them in the finalized string, adds the replace character <, takes out the (,) from the front of the temp string, which leaves characters 54 and on. so then it does the same thing, searches via indexOf, for (,) and if it finds anything, it does it all over again.


    Mostly i am wondering is this the best way to do this in flash? It will get the job done, but seeing as i am be searching big big strings, full blown articles, i want to make sure i wont bog it down completely. Or who knows, i dont know flash that well so there may be something built in to do this, i just didnt see it in "String".


    Thanks to any comments, and hopefully you understand what i am trying to say lol.

  2. #2
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    Maybe something like this....

    code:

    myText = "this is some text with a comma, so it can be removed with the string replace proto.";
    String.prototype.stripSign = function() {
    return this.split(",").join("");
    }
    s=myText.stripSign();
    trace(s);


  3. #3
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    Well if i read that right, that just removes characters right? I am looking for a "replace" thing.

    Or i take that back, i am looking for comments on the best way to do it. I already made a replace function, but seeing as it will be scanning very large strings, i want to make sure it will be doing it efficiently.

  4. #4
    Senior Member
    Join Date
    Sep 2000
    Location
    Pittsburgh
    Posts
    252
    Hi Zeusbwr,

    what NTD said should do pretty much what you need, but in your case it would look like:

    Code:
    var str = "here is a link (,)href='http://www.flashkit.com'>flashkit(,)/a>";
    var stuff = str.split("(,)").join("<");
    trace(stuff);
    //trace : here is a link <href='http://www.flashkit.com'>flashkit</a>
    at least i think that's what your conveying.

    If so, and your articles are huge you may bog down the player and your xml will be hard to read. If your concern is dealing with html tags in xml you can bypass all this jazz and use CDATA tags in your xml. Example...

    XML:
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <data>
    <article><![CDATA[<font color="#000000" size="14">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation <font color="#ff00ff"><a href='http://www.flashkit.com'>flashkit</a></font> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</font>]]></article>
    </data>
    flash:
    Code:
    var myxml = new XML();
    myxml.ignoreWhite = true;
    myxml.onLoad = doStuff;
    
    myxml.load("test.xml");
    
    function doStuff(){
    	_root.createTextField('test', _root.getNextHighestDepth(), 25, 25, 200, 10);
    	test.autoSize = true;
    	test.wordWrap = true
    	test.html = true;
    	var rootNode = this.firstChild
    	var article = rootNode.firstChild.firstChild.nodeValue
    	test.htmlText = article;
    }
    this all relies on the fact that i understood what your trying to do. :-)

    hope this helps some,
    Dunc

  5. #5
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    Man, i believe the CDATA is exactly what i need. Like i said i made my own String parser thing, not as efficient as his by the looks of it, but i am new to what flash has to offer.

    None the less it seems like the CDATA is a easier way to go, there is no special parsing with CDATA right?

    None the less, i'll give it a try tomorrow (sat), big thanks!

  6. #6
    Senior Member
    Join Date
    Sep 2000
    Location
    Pittsburgh
    Posts
    252
    there shouldn't be any special parsing, although you may need to url encode some things for special characters like ™ ® © and so forth, i can't be 100% on that though as it's been a while and i tend to forget. but it's an easier way to deal with it and your xml files should be useable and readable outside of the flash world.

    see link for url encoding..

    urlencoding

    dunc

  7. #7
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    The only other thing I can think of that you might want to watch out for using CDATA is if your XML was like this,

    <node>text</node>

    you might extract the text in actionscript using,

    text = aReferenceToNode.firstChild;

    whereas if you used CDATA,

    <node><![CDATA[text]]></node>

    you would need to modify the actionscript like this,

    text = aReferenceToNode.firstChild.nodeValue;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center