新手在这里:))
首先是我的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从flash输入文本字段发送到mySQL表。然后,当flash再次加载它时,我的flash文本字段中的所有网址都将是可点击的。
当然我可以在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);
但问题是这个字符串在flash中是可编辑的,所以当我将它(编辑)发送回PHP时,PHP会覆盖链接并使它们对flash不可读(例如):
<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函数来检查来自flash的已发送数据是否已包含可点击的url,但是如果我需要在编辑后的字符串中添加另一个链接,那么preg_replace就不会激活...
有什么办法可以做我需要的吗?
提前致谢, 阿图尔。
答案 0 :(得分:1)
[[编辑]]
第32行改为:
last = lowest.end;
为:
last = lowest.end + idx;
正如我在评论中提到的......真的把代码吹了一下。另一种方法是引入现有的AS2正则表达式库。最后我看了(5年前),关于表达式支持,AS2正则表达式实现不是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合作过了一段时间......
祝你好运!
答案 1 :(得分: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;
}
?>