title: Regex for preg_preplace urls with proper URLs only
body: <p>I have basic knowledge in regex and I have managed to do a url detect and replace with html '' tag link.  Currently I have a working code; currently I detect links like following</p>
  
  <pre><code>    http://google.com
      https://google.com
      www.google.com
      https://www.google.com
      google.com
  </code></pre>
  
  <p>What I am trying to achieve is avoid 'google.com' from detecting as a URL because if it detect 'google.com' as a URL, it will also detect a text/name like 'rameez.rami'.</p>
  
  <pre><code>&lt;?php
  $string = ' rameez.rami https://www.google.co.in/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=migrate+from+mysql+to+oracle plz use http:// links instead of https:// links and http:hi all plse chec http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx http://www.google.com https://www.google.com https://www.google.com http://google.com https://google.com google.com';
  
          $urlRegex = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
          $result = preg_replace($urlRegex, '&lt;a href="$0"&gt;$4&lt;/a&gt;', $string);
          print_r($result);
          die;
  ?&gt;
  </code></pre>
  
  <p>Current output is: </p>
  
  <pre><code>&lt;a href="rameez.rami"&gt;rameez.rami&lt;/a&gt; &lt;a href="https://www.google.co.in/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=migrate+from+mysql+to+oracle"&gt;www.google.co.in/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=migrate+from+mysql+to+oracle&lt;/a&gt; plz use http:// links instead of https:// links and http:hi all plse chec &lt;a href="http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx"&gt;teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx&lt;/a&gt;  &lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt; &lt;a href="https://www.google.com"&gt;www.google.com&lt;/a&gt; &lt;a href="https://www.google.com"&gt;www.google.com&lt;/a&gt; &lt;a href="http://google.com"&gt;google.com&lt;/a&gt; &lt;a href="https://google.com"&gt;google.com&lt;/a&gt; &lt;a href="google.com"&gt;google.com&lt;/a&gt;
  </code></pre>
  
  <p>My expected out:</p>
  
  <pre><code>rameez.rami &lt;a href="https://www.google.co.in/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=migrate+from+mysql+to+oracle"&gt;www.google.co.in/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=migrate+from+mysql+to+oracle&lt;/a&gt; plz use http:// links instead of https:// links and http:hi all plse chec &lt;a href="http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx"&gt;teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx&lt;/a&gt; &lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt; &lt;a href="https://www.google.com"&gt;www.google.com&lt;/a&gt; &lt;a href="https://www.google.com"&gt;www.google.com&lt;/a&gt; &lt;a href="http://google.com"&gt;google.com&lt;/a&gt; &lt;a href="https://google.com"&gt;google.com&lt;/a&gt; google.com
  </code></pre>