actionscript find and convert text to url

Posted by gravesit on Stack Overflow See other posts from Stack Overflow or by gravesit
Published on 2010-04-30T18:26:55Z Indexed on 2010/04/30 18:27 UTC
Read the original article Hit count: 126

Filed under:
|

I have this script that grabs a twitter feed and displays in a little widget. What I want to do is look at the text for a url and convert that url to a link.

    public class Main extends MovieClip
{
    private var twitterXML:XML; // This holds the xml data

    public function Main()
    {
        // This is Untold Entertainment's Twitter id.  Did you grab yours?
        var myTwitterID= "username"; 
        // Fire the loadTwitterXML method, passing it the url to your Twitter info:
        loadTwitterXML("http://twitter.com/statuses/user_timeline/" + myTwitterID + ".xml");
    }

    private function loadTwitterXML(URL:String):void
    {
        var urlLoader:URLLoader = new URLLoader();
        // When all the junk has been pulled in from the url, we'll fire finishedLoadingXML:
        urlLoader.addEventListener(Event.COMPLETE, finishLoadingXML);
        urlLoader.load(new URLRequest(URL));            
    }

    private function finishLoadingXML(e:Event = null):void
    {
        // All the junk has been pulled in from the xml!  Hooray!
        // Remove the eventListener as a bit of housecleaning:
        e.target.removeEventListener(Event.COMPLETE, finishLoadingXML);

        // Populate the xml object with the xml data:
        twitterXML = new XML(e.target.data);
        showTwitterStatus();
    }

    private function addTextToField(text:String,field:TextField):void{
       /*Regular expressions for replacement, g: replace all, i: no lower/upper case difference
       Finds all strings starting with "http://", followed by any number of characters
       niether space nor new line.*/
       var reg:RegExp=/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
       //Replaces Note: "$&" stands for the replaced string.
       text.replace(reg,"<a href=\"$&\">$&</a>");
       field.htmlText=text;
    }

    private function showTwitterStatus():void
    {
        // Uncomment this line if you want to see all the fun stuff Twitter sends you:
        //trace(twitterXML);

        // Prep the text field to hold our latest Twitter update:
        twitter_txt.wordWrap = true;
        twitter_txt.autoSize = TextFieldAutoSize.LEFT;

        // Populate the text field with the first element in the status.text nodes:
        addTextToField(twitterXML.status.text[0], twitter_txt);
    }

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about regex