views:

95

answers:

1

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);
    }
A: 

If this

/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig

is your regexp for converting text to urls, than i have some remarks.

First of all - almost all characters in chacacter classes are parsed literaly. So, here

[-A-Z0-9+&@#\/%?=~_|!:,.;]

you say to search any of this characters (except /).

Simple regexp for url search will look similar to this

/\s((https?|ftp|file):\/\/)?([-a-z0-9_.:])+(\?[-a-z0-9%_?&.])?(\s+|$)/ig

Im not sure, if it will porcess url borders right, but \b symbol can be a dot, so i think \s (space or line braker) will suit better. Im not sure about ending (is it allowed in actionscript to use end-of-string symbol not at the end of regexp) And ofcourse, you have to tune it to suit your data.

Azilot