title: Automatic link detector (PHP + RegEx)
body: <p>I've created a function that automatically detects links. It works quite fine, except when a HTML tag begins just after a link (without space).</p>
  
  <p>Example :</p>
  
  <pre><code>http://www.google.com is cool
  </code></pre>
  
  <p>becomes</p>
  
  <pre><code>&amp;lt;a href="http://www.google.com" target="_blank"&amp;gt;www.google.com&amp;lt;/a&amp;gt; is cool
  </code></pre>
  
  <p>but</p>
  
  <pre><code>http://www.google.com&amp;lt;br /&gt;is cool
  </code></pre>
  
  <p>(without space after http:// in real) becomes</p>
  
  <pre><code>&amp;lt;a href="http://www.google.com&amp;lt;br /" target="_blank"&amp;gt;www.google.com&amp;lt;br /&amp;lt;/a&amp;gt; is cool
  </code></pre>
  
  <p>My regular expression doesn't stop at first <code>&lt;</code>, though <code>&lt;</code> is not allowed...</p>
  
  <p>How can I fix that ? Or how can I exclude <code>&lt;</code> efficiently ?</p>
  
  <p>Here's my function :</p>
  
  <pre><code>function detect_linkg($str){        
      return preg_replace('#(https?://)([\w\d.&amp;:\#@%/;$~_?\+-=]*)#','&lt;a href="$1$2" target="_blank"&gt;$2&lt;/a&gt;',$str);
  }
  </code></pre>
  
  <p>Thanks for your help !</p>