title: How to add part to regex replace?
body: <p>I am trying to automatically detect links in content and create hyperlinks.</p>
  
  <p>I support <code>http</code>, <code>https</code> and <code>www</code>.</p>
  
  <p>The problem is, that when my regex find the <code>www</code> link, it should prepend <code>http://</code> to the result.</p>
  
  <p>I know I could use a <code>str_replace()</code> or <code>strpos()</code> etc. but can this be done using the <code>preg_replace()</code>?</p>
  
  <h2>PHP</h2>
  
  <pre><code>$input = '&lt;p&gt;Hello, visit http://stackoverflow.com or www.stackoverflow.com&lt;/p&gt;';
  $regex_url = '~(?:(https?)://([^\s&lt;]+)|(?:www\.[^\s&lt;]+?\.[^\s&lt;]+))(?&lt;![\.,:])~i';
  echo preg_replace( $regex_url, '&lt;a href="$0" target="_blank"&gt;$0&lt;/a&gt;', $input );
  </code></pre>
  
  <h2>Result</h2>
  
  <pre><code>&lt;p&gt;Hello, visit &lt;a href="http://stackoverflow.com" target="_blank"&gt;http://stackoverflow.com&lt;/a&gt; or &lt;a href="www.stackoverflow.com" target="_blank"&gt;www.stackoverflow.com&lt;/a&gt;&lt;/p&gt;
  </code></pre>