title: PHP REGEX to convert links to hyperlink in string - remove last period
body: <p>I have the following code to convert links to hyperlinks in a string.</p>
  
  <pre><code>$text_block = preg_replace('$(\s|^)(https?://[a-z0-9_./?=&amp;-]+)(?![^&lt;&gt;]*&gt;)$i', ' &lt;a href="$2" target="_blank"&gt;$2&lt;/a&gt; ',$text_block);
  </code></pre>
  
  <p>However, if the link as a period at the end, like "do a search on <a href="http://google.com" rel="nofollow">http://google.com</a>.", the regex includes the period in the link. How can I change the regex above to look for and not include a period if present?</p>
  
  <p>EDIT: For clarification - the <code>$text_block</code> is a large block of text that may contain many links. The regex need to parse through the block of text and find and convert all found links.</p>
  
  <p>EDIT 2: As pointed out below in the comments, I guess you'd have to account for domains like ".co.uk". So I guess it would have to look for and remove the last period that is followed by whitespace, if present... gets tricky. Any ideas?</p>