新手在這裏:)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。
如果應用程序可以在AS3寫,你可以複製你的正則表達式,並在你的閃片使用它,否則,問題關於findURL基於循環中的return語句。你是第一次回來,因此它從來沒有打過第二次。 – stat
它不能寫在AS3中,我的整個項目寫在AS2 :( –
mmm ...增加幾行代碼... 1分鐘:P – stat