title: Regular Expression for urls for images and links
body: <p>EDIT: I'm not parsing html like the 5 billion other questions that have been posted. This is raw unformatted text that I want to convert into some HTML.</p>
  
  <p>I'm working on a post processing. I need to convert Urls with image endings (jpe?g|png|gif) into image tags, and all other Urls into href links. I have my image replacement correct, however I'm stuck keeping the link replacement from trying to overwrite one another.</p>
  
  <p>I need help with the expression within how to get it to looked for urls without the tags in place from the image replace, or look for urls that do not end in dot jpe?g|png|gif.</p>
  
  <pre><code>public function smartConvertPost($post) {
  
      /**
       * Match image based urls
       */
      $pattern = '!http://([a-z0-9\-\.\/\_]+\.(?:jpe?g|png|gif))!Ui';
      $replace='&lt;p&gt;&lt;img src="http://$1"&gt;&lt;/p&gt;';
      $postImages = preg_replace($pattern,$replace,$post);
  
      /**
       * Match url based
       */
      $pattern='/http://([a-z0-9\-\.\/\_]+(?:\S|$))/i';
      $replace='&lt;a href="$1"&gt;$1&lt;/a&gt;';
      $postUrl = preg_replace($pattern,$replace, $postImages);
  
  return $postUrl;
  }
  </code></pre>
  
  <p>Please note I am not talking about matching tags or html. matching a string like so and converting it to html.</p>
  
  <pre><code>If this was an example post with a Url to a page like http://www.some-website.com/some-page/anything.html and I also put a url to an image http://www.some-website.com/someimage.jpg you would need to regex the two to be a hyperlink and an image. 
  </code></pre>
  
  <p>Thanks,</p>