2011-11-27 205 views
1

新手在這裏:)AS2多字符串替換

首先我AS2代碼:

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

輸出:

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 

首先的問題是,爲什麼我的閃光功能始終僅更換第一網址是什麼?

我想要做的是將字符串從Flash輸入文本字段由PHP發送到mySQL表。然後,當flash再次加載它時,我的flash文本字段中的所有URL都將可點擊。

當然,我可以在PHP中使用的preg_replace作者:

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

但問題是,這串閃光可編輯的,所以當我把它(編輯)回到PHP,PHP覆蓋鏈接,使他們不可讀的閃存(例如):

<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> 

我也可以使用一些PHP函數,它會檢查是否從閃存sended數據已經包含可點擊的鏈接的,但如果我需要添加在編輯字符串另一個鏈接符,preg_replace不是那麼火。 ..

有什麼辦法可以做我需要的嗎?

在此先感謝, Artur。

+0

如果應用程序可以在AS3寫,你可以複製你的正則表達式,並在你的閃片使用它​​,否則,問題關於findURL基於循環中的return語句。你是第一次回來,因此它從來沒有打過第二次。 – stat

+1

它不能寫在AS3中,我的整個項目寫在AS2 :( –

+0

mmm ...增加幾行代碼... 1分鐘:P – stat

回答

1

[[編輯]]

線32從:更改

last = lowest.end; 

到:

last = lowest.end + idx; 

正如我在評論中提及了...真的吹碼出一點。另一種方法是引入現有的AS2正則表達式庫。最後我看了(5年前),AS2 regexp實現並不是100%的表達支持。我敢肯定有一個更短的方式去做這件事,但這是第一次嘗試後的代碼:

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

讓我知道是否有任何問題。 findLowestSelector方法使用單個空格或行尾來指定返回對象中的結束值。

過去了一段時間,因爲我已經與AS2合作過...

祝你好運!

+0

非常感謝您花費我的時間:)我會檢查一下,並告訴您是否可以在我的項目中對此做些什麼。 –

+0

到目前爲止,我可以看到您的代碼正確返回最大2個鏈接。取決於字符串中存在多於2個(超過2個)的鏈接 - 鏈接開始循環和拉動自己。但這是一個很好的起點。由於它適用於2個鏈接,因此它也適用於多鏈接,所以我會研究你的代碼,試着找出你在這裏完成的魔法。再次感謝。 –

+0

很高興有幫助。它將與儘可能多的鏈接一起使用。有一些限制,因爲它需要鏈接之間至少有一個空格。如果沒有任何問題,您可以請upvote並接受我的答案嗎? :) – stat

0

找到其他解決方案 - 由PHP。

功能between_replace了那裏:http://dk2.php.net/manual/en/function.str-replace.php#104072

代碼:

<? 
$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; 
} 
?>