2011-11-27 206 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; 
} 
?>