Search a string for html links

Hi There,

Just wondering if anyone knows of a way to search a string for html links, the reason i ask is so that when my clients update their sites using external text files, all they have to do is type in www.somewherenice.com and when the var is imported into flash it will be converted into <a href=“http://www.somewherenice.com”>www.somewherenice.com</a> so my clients don’t need to learn lots of html coding.

Cheers,

Bob
BC DESIGN
www.bobcooper.org.uk

no. I mean when you put 3 or more links in… it kills it. i did not have the HTML link

Hi Nathan,

I see what you mean… I’ve re-written the code once more, hopefully it should be able to have any number of links/emails in any order. you can also use the same email more than once in the text (this was what caused the previous code to crash)… let me know if you have anymore probs and if anyone thinks they can trim down the code then please let me know… here’s the code…

// link check vars
var searchPos:Number = 0;
var textStringArray:Array = null;
var textString:String = "info@bob.com mail@somehwere.com hello this is some normal text email@me.co.uk www.bob.com hello www.bananas.com  info@bob.com www.bob.com www.bob.com www.bob.co.uk emails in mail@somewherenic.com just make sure you bob@bob.com have a space after the links! www.bob.com";
var linkStart:Number = 0;
var linkStop:Number = 0;
var linkURL:String = "";
var linkHREF:String = "";
var textLength:Number = 0;
var linkArray:Array = new Array();
var emailArray:Array = new Array();
var atPos:Number = 0;
var emailStart:Number = 0;
var emailStop:Number = 0;
var emailURL:String = "";
var emailHREF:String = "";
var linkCount:Number = 0;
var emailCount:Number = 0;
var emailUsed:Boolean = false;
var linkUsed:Boolean = false;

linkCheck(textString);

// check text for links and replace
function linkCheck(textString):void{
    searchPos = 0;
    findLink();
    
    // check for www and replace with href link
    function findLink():void{
        textLength = textString.length;
        textStringArray = textString.split("");
        linkStart = textString.indexOf("www", searchPos);
        linkStop = textString.indexOf(" ", linkStart);
        linkURL = "";
        for(var i =linkStart; i < linkStop; i++){
            linkURL += textStringArray*;
            
        }
        linkHREF = "<a href=\"http://" + linkURL + " \">" + linkURL + " </a>";
        // check linkArray to ensure current link has not already been swapped
        for(var j = 0; j < linkArray.length; j++){
            if(linkArray[j] == linkURL){
                linkUsed = true;
            }
        }
        if(linkUsed == false){
            textString = textString.split(linkURL).join(linkHREF);
            // add linkURL to the array of swapped URL's
            linkArray[linkCount] = linkURL;
            linkCount++;
        } else {
            linkUsed = false;
        }
        searchPos = linkStart + linkHREF.length - 16;
        if(textString.indexOf("www", searchPos) != -1){
            findLink();
        } else {
            searchPos = 0;
            findEmail();
        }
        
    }
    
    // check for emails and replace with mailto link
    function findEmail():void{
        textLength = textString.length;
        textStringArray = textString.split("");
        atPos = textString.indexOf("@", searchPos);
        emailStart = textString.lastIndexOf(" ", atPos) + 1;
        emailStop = textString.indexOf(" ", emailStart);
        emailURL = "";
        for(var i =emailStart; i < emailStop; i++){
            emailURL += textStringArray*;
        }
        // delete href from links that are already links
        emailURL = emailURL.split("href=\"mailto:").join("");
        emailHREF = "<a href=\"mailto:" +emailURL + " \">" + emailURL + " </a>";
        // check emailArray to ensure current email has not already been swapped
        for(var j = 0; j < emailArray.length; j++){
            if(emailArray[j] == emailURL){
                emailUsed = true;
            }
        }
        if(emailUsed == false){
            textString = textString.split(emailURL).join(emailHREF);
            // add linkURL to the array of swapped URL's
            emailArray[emailCount] = emailURL;
            emailCount++;
        } else {
            emailUsed = false;
        }
        searchPos = emailStart + emailHREF.length;
        if(textString.indexOf("@", searchPos) != -1){
            findEmail();
        } else {
            inputText(textString);
        }
    }
}

// insert loaded text
function inputText(loadedText):void {
    trace(loadedText);
}

Once again this is in AS3 code but just change the voids for AS2

Cheers,

Bob