1
votes

Newbie here :)

First my AS2 code:

txt.html=true;
txt.htmlText="This is an example: www.sample.com is not www.othersample.com";
var sampleText:String=findUrl(txt.text);
trace(sampleText);
function findUrl(str){
    var rawURL:Array = new Array();
    rawURL = str.split(' ');
    for(var i=0; i<rawURL.length; i++) {
        if(rawURL[i].indexOf("http://") != -1 or rawURL[i].indexOf("www.") != -1)    {
            return (str.replace(rawURL[i], "<a href='"+rawURL[i]+"' target='_blank'><u><font color='#666666'>"+rawURL[i]+"</font></u></a>"));
        }
    }
}

Output:

This is an example: <a href='www.sample.com' target='_blank'><u><font color='#666666'>www.sample.com</font></u></a> is not www.othersample.com

First question is why my flash function always replace only first url?

What I'm trying to do is send string from flash input textfield by PHP to mySQL table. Then, when flash will load it again all urls in my flash textfield will be clickable.

Of course I can use preg_replace in PHP:

$comments = $_POST['comments'];
$comments = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2", $comments);
$comments = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i", "<u><A TARGET=\"_blank\" HREF=\"$1\"><font color=\"#666666\">$1</font></A></u>", $comments);

but the problem is that this string is editable in flash, so when I send it (edited) back to PHP, PHP overwriting links and make them unreadable for flash (example):

<u><a target="_blank" href="<u><a target="_blank" href="http://asdasd.asdasd.pl"><font color="#666666">http://asdasd.asdasd.pl</font></a></u>"><font color="#666666"><u><a target="_blank" href="http://asdasd.asdasd.pl"><font color="#666666">http://asdasd.asdasd.pl</font></a></u></font></a></u>

I can also use some PHP function which will check whether sended data from flash already contains clickable url's, but if I need add another link in edited string, preg_replace not fire then...

Is there any way to do what I need?

Thanks in advance, Artur.

2
If the app can be written in AS3, you can duplicate your regexp and use it in your flash piece, otherwise, the issue regarding findURL is based on the return statement in your loop. You are returning at the first instance, thus it never hits the second occurrence.stat
It can't be writen in AS3, my whole project is writen in AS2 :(Artur Filipiak
mmm...adds a few lines of code...1 min :Pstat
If you have source file... Unfortunately my project is a flash module for AS2 project to which I have no FLA file ;)Artur Filipiak

2 Answers

1
votes

[[EDIT]]

Line 32 changed from:

last = lowest.end;

to:

last = lowest.end + idx;

As I mentioned in the comment...really blows the code out a little. The alternative is to pull in an existing AS2 regexp library. Last I looked (5 years ago), the AS2 regexp implementations were not 100% with regards to expression support. I'm sure there is a shorter way of going about this, but here is the code after a first attempt:

String.prototype.replace = function(search:String, replace:String):String
{
    return this.split(search).join(replace);
}

function linkPaths(text:String, template:String):String
{
    var idx:Number;
    var lowest:Object;

    var selectors:Array =
    [
        "http://",
        "www."
    ];

    var sub:String;
    var last:Number = 0;
    var result:String = "";

    for(idx=0; idx<text.length; idx++)
    {
        sub = text.substring(idx);
        lowest = findLowestSelector(sub, selectors);

        if(!lowest)
        {
            break;
        }

        result += text.substring(last, lowest.start + idx) + template.replace("${url}", sub.substring(lowest.start, lowest.end));
        last = lowest.end + idx;
        idx += lowest.end;
    }

    return result;
}

function findLowestSelector(text:String, selectors:Array):Object
{
    var idx:Number;
    var index:Number;

    var start:Number = Number.MAX_VALUE;
    var end:Number = -1;

    for(idx=0; idx<selectors.length; idx++)
    {
        index = text.indexOf(selectors[idx]);

        if
        (
            index != -1 &&
            index < start
        )
        {
            start = index;

            index = text.indexOf(" ", start);
            end = index == -1 ? text.length : index ;
        }
    }

    return start != -1 ?
        {
            start: start,
            end: end
        }
        :
        null
    ;
}

trace(linkPaths
(
    "test text here http://www.test.com is a link, along with www.test.com",
    "<a href='${url}' target='_blank'><u><font color='#666666'>${url}</font></u></a>"
));

Let me know if there are any issues. The findLowestSelector method uses a single blank space, or end of line to dictate the end value in the return object.

Been a while since I've worked with AS2...

Best of luck!

0
votes

Found also other solution - by PHP.

function between_replace got there: http://dk2.php.net/manual/en/function.str-replace.php#104072

Code:

<?
$sample=$_POST['sample'];

$sample = ereg_replace("[\]", "", $sample);
between_replace ('<u><a target="_blank" href="','">', $sample, "");
$sample = str_replace('<u><a target="_blank" href="', "", $sample);
$sample = str_replace('"><font color="#666666">http://', "", $sample);
$sample = str_replace("</font></a></u>", "", $sample);
$sample = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2", $sample);
$sample = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i", "<u><a target=\"_blank\" href=\"$1\"><font color=\"#666666\">$1</font></a></u>", $sample);
$sample = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<u><a href=\"mailto:$1\"><font color=\"#666666\">$1</font></a></u>",$sample); 
$sample = ereg_replace("[\n]", "\n", $sample);
$sample = ereg_replace("[']", "''", $sample);

echo "&sampletext=" . $sample."&";

function between_replace ($open, $close, &$in, $with, $limit=false, $from=0)
{
if ($limit!==false && $limit==0)
{
    return $in;
}        
$open_position = strpos ($in, $open, $from);
if ($open_position===false)
{
    return false;
};
$close_position = strpos ($in, $close, $open_position+strlen($open));
if ($close_position===false)
{
    return false;
};
$current = false;
if (strpos($with,'{*}')!==false)
{
    $current = substr ($in, $open_position+strlen($open), $close_position-$open_position-strlen($open));
    $current = str_replace ('{*}',$current,$with);
    //debug_echo ($current);
}
else
{
    $current = $with;
}
$in = substr_replace ($in, $current, $open_position+strlen($open), $close_position-$open_position-strlen($open));
$next_position = $open_position + strlen($current) + 1;
if ($next_position>=strlen($in))
{
    return false;
}
if ($limit!==false)
{
    $limit--;
}        
between_replace ($open, $close, $in, $with, $limit, $next_position);
return $in;
}
?>