title: AS2 Multi string replace
body: <p>Newbie here :)</p>
  
  <p>First my AS2 code:</p>
  
  <pre><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&lt;rawURL.length; i++) {
          if(rawURL[i].indexOf("http://") != -1 or rawURL[i].indexOf("www.") != -1)    {
              return (str.replace(rawURL[i], "&lt;a href='"+rawURL[i]+"' target='_blank'&gt;&lt;u&gt;&lt;font color='#666666'&gt;"+rawURL[i]+"&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;"));
          }
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>This is an example: &lt;a href='www.sample.com' target='_blank'&gt;&lt;u&gt;&lt;font color='#666666'&gt;www.sample.com&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; is not www.othersample.com
  </code></pre>
  
  <p>First question is why my flash function always replace only first url?</p>
  
  <p>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.</p>
  
  <p>Of course I can use preg_replace in PHP:</p>
  
  <pre><code>$comments = $_POST['comments'];
  $comments = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2", $comments);
  $comments = preg_replace("/([\w]+:\/\/[\w-?&amp;;#~=\.\/\@]+[\w\/])/i", "&lt;u&gt;&lt;A TARGET=\"_blank\" HREF=\"$1\"&gt;&lt;font color=\"#666666\"&gt;$1&lt;/font&gt;&lt;/A&gt;&lt;/u&gt;", $comments);
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>&lt;u&gt;&lt;a target="_blank" href="&lt;u&gt;&lt;a target="_blank" href="http://asdasd.asdasd.pl"&gt;&lt;font color="#666666"&gt;http://asdasd.asdasd.pl&lt;/font&gt;&lt;/a&gt;&lt;/u&gt;"&gt;&lt;font color="#666666"&gt;&lt;u&gt;&lt;a target="_blank" href="http://asdasd.asdasd.pl"&gt;&lt;font color="#666666"&gt;http://asdasd.asdasd.pl&lt;/font&gt;&lt;/a&gt;&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;/u&gt;
  </code></pre>
  
  <p>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... </p>
  
  <p>Is there any way to do what I need?</p>
  
  <p>Thanks in advance,
  Artur.</p>