title: PHP URL to Link with Regex
body: <p>I know I've seen this done a lot in places, but I need something a little more different than the norm. Sadly When I search this anywhere it gets buried in posts about just making the link into an html  tag link. I want the PHP function to strip out the "http://" and "https://" from the link as well as anything after the .* so basically what I am looking for is to turn A into B.</p>
  
  <pre><code>A: http://www.youtube.com/watch?v=spsnQWtsUFM
  B: &lt;a href="http://www.youtube.com/watch?v=spsnQWtsUFM"&gt;www.youtube.com&lt;/a&gt;
  </code></pre>
  
  <p>If it helps, here is my current PHP regex replace function.</p>
  
  <pre><code>ereg_replace("[[:alpha:]]+://[^&lt;&gt;[:space:]]+[[:alnum:]/]", "&lt;a href=\"\\0\" class=\"bwl\" target=\"_new\"&gt;\\0&lt;/a&gt;", htmlspecialchars($body, ENT_QUOTES)));
  </code></pre>
  
  <p>It would probably also be helpful to say that I have absolutely no understanding in regular expressions. Thanks!</p>
  
  <p>EDIT: When I entered a comment like this <code>blahblah <a href="https://www.facebook.com/?sk=ff&amp;ap=1" rel="nofollow">https://www.facebook.com/?sk=ff&amp;ap=1</a> blah</code> I get html like this<code>&lt;a class="bwl" href="blahblah <a href="https://www.facebook.com/?sk=ff&amp;amp;ap=1" rel="nofollow">https://www.facebook.com/?sk=ff&amp;amp;ap=1</a> blah"&gt;www.facebook.com&lt;/a&gt;</code> which doesn't work at all as it is taking the text around the link with it. It works great if someone only comments a link however. This is when I changed the function to this </p>
  
  <pre><code>preg_replace("#^(.*)//(.*)/(.*)$#",'&lt;a class="bwl" href="\0"&gt;\2&lt;/a&gt;',  htmlspecialchars($body, ENT_QUOTES));
  </code></pre>