title: Match URL pattern in PHP using a regular expression
body: <p>I want to match a URL link in a wall post and replace this link with anchor tag. For this I use the regular expression below.</p>
  <p>I would like the match four types of URL:</p>
  <ol>
  <li><code>http://example.com</code></li>
  <li><code>https://example.com</code></li>
  <li><code>www.example.com</code></li>
  <li><code>example.com</code></li>
  </ol>
  
  <pre><code>preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
               '&lt;a href=&quot;$1&quot;&gt;$1&lt;/a&gt;', $subject);
  </code></pre>
  <p>This expression matches only first two types of URL.</p>
  <p>If I use this expression for matching a URL pattern,
  <code>'@(www?([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@'</code>, it only matches the third type of URL pattern.</p>
  <p>How can I match all four typeS of URL patternS with a single regular expression?</p>