Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Brad Christie's <code>preg_replace_callback()</code> recommendation is a good one. Here is one possible implementation:</p> <pre class="lang-php prettyprint-override"><code>function smartConvertPost($post) { // Disclaimer: This "URL plucking" regex is far from ideal. $pattern = '!http://[a-z0-9\-._~\!$&amp;\'()*+,;=:/?#[\]@%]+!i'; $replace='_handle_URL_callback'; return preg_replace_callback($pattern,$replace, $post); } function _handle_URL_callback($matches) { // preg_replace_callback() is passed one parameter: $matches. if (preg_match('/\.(?:jpe?g|png|gif)(?:$|[?#])/', $matches[0])) { // This is an image if path ends in .GIF, .PNG, .JPG or .JPEG. return '&lt;p&gt;&lt;img src="'. $matches[0] .'"&gt;&lt;/p&gt;'; } // Otherwise handle as NOT an image. return '&lt;a href="'. $matches[0] .'"&gt;'. $matches[0] .'&lt;/a&gt;'; } </code></pre> <p>Note that the regex used to pluck out a URL is not ideal. To do it right is tricky. See the following resources:</p> <ul> <li><a href="http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html" rel="nofollow">The Problem With URLs</a> by Jeff Atwood.</li> <li><a href="http://daringfireball.net/2010/07/improved_regex_for_matching_urls" rel="nofollow">An Improved Liberal, Accurate Regex Pattern for Matching URLs</a> by John Gruber.</li> <li><a href="http://jmrware.com/articles/2010/linkifyurl/linkify.html" rel="nofollow">URL Linkification (HTTP/FTP).</a> by yours truly.</li> </ul> <p><strong>Edit:</strong> Added ability to recognize image URLs having a query or fragment.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload