title: how to REGEX a url and autoadd [url] tag and fix repeat
body: <p>I need a function to auto add bbcode.</p>
  
  <ol>
  <li>users use my editor type something.</li>
  <li>after submit, will be run a autolink() php function.</li>
  <li>if content text have url will be auto add [url] xxx [/url]</li>
  </ol>
  
  <p>I can do it. But the bbcode editor maybe use 2 format url</p>
  
  <p>A. [url]xx[/url]
  B. [url=xxx] xxx[/url]</p>
  
  <p>I can't fix repeat problem. here's demo code</p>
  
  <pre><code>$content="
  http://www.google.com/logo.png
  
  [url]http://www.yahoo.com[/url]
  
  [url=http://yahoo.com/abc.zip]DownLoad[/url]";
  
  $content = nl2br($content);
  echo linkAdd($content);
  
  
      //auto add link
      function linkAdd($content){
  
          preg_match_all('~\[url=((?:ftp|https?)://.*?)\](.*?)\[/url\]~s', $content, $stack);        
          foreach($stack[1] as $t) {
             $content = preg_replace('~\[url=((?:ftp|https?)://.*?)\](.*?)\[/url\]~s','[url=\\1]\\2[/url]', $content,1); 
          }
  
          preg_match_all('~\[url](.*?)\[/url\]~s', $content, $stack);        
          foreach($stack[1] as $t) {
             $content = preg_replace('~\[url](.*?)\[/url\]~s',$t, $content,1); 
          }
  
          $str=preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&amp;//=]+)','[url]\\0[/url]',$content);           
  
          return $str;
      }
  </code></pre>
  
  <p>Now Results</p>
  
  <pre><code>[url]http://www.google.com/logo.png[/url]
  
  [url]http://www.yahoo.com[/url]
  
  [url=[url]http://yahoo.com/abc.zip[/url]]DownLoad[/url]
  </code></pre>
  
  <p>line one and line two is ok.</p>
  
  <p>but line 3 repeat again [url]XXX[/url]</p>
  
  <pre><code>how to be 
  [url=[url]http://yahoo.com/abc.zip[/url]]DownLoad[/url]
  to
  [url=http://yahoo.com/abc.zip]DownLoad[/url]
  </code></pre>