2010-04-30 5 views
0

Possible en double:
How do I linkify text using ActionScript 3actionscript trouver et convertir le texte à l'URL

J'ai ce script qui récupère un flux twitter et affiche dans un petit widget. Ce que je veux faire est de regarder le texte pour une url et de convertir cette URL en un lien.

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

Répondre

0

Si cette

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

est votre regexp pour convertir du texte à urls, que moi quelques remarques. Tout d'abord, presque tous les caractères dans les classes de caractères sont analysés littéralement.

Donc, ici

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

vous dites de rechercher tous ces personnages (sauf /).

simple expression rationnelle pour la recherche d'URL ressemblera à ce

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

Je ne sais pas, si elle traitera les frontières url droite, mais symbole \b peut être un point, donc je pense que \s (espace ou linebreak) conviendra mieux.

`m pas sûr de fin (est-il permis en actionscript d'utiliser le symbole de fin de chaîne pas à la fin de regexp?)

Et, bien sûr, il faut le régler en fonction de votre Les données.

Questions connexes