login/register

Snip!t from collection of Alan Dix

see all channels for Alan Dix

Snip
summary

In late October Jeff Atwood wrote about The Problems Wit...
My website is at http://josephscott.org/
Would be changed into:
My website is at <a href=’http://josephscott.org/⠮..
... 01.

MakeItLink – Detecting URLs In Text And Making Them Links || Joseph Scott
http://josephscott.org/...keitlink-detecting-urls-in-text-and-making-them-links/

Categories

/Channels/techie/web development

[ go to category ]

For Snip

loading snip actions ...

For Page

loading url actions ...

In late October Jeff Atwood wrote about The Problems With URLs, describing the problems of parsing out URLs in text and transforming them into links. Here’s a simple example:

My website is at http://josephscott.org/

Would be changed into:

My website is at <a href=’http://josephscott.org/’>http://josephscott.org/</a>

Sounds simple right? Once you start looking at what the valid character set is for URLs things get tricky. I won’t rehash all of items, go the The Problem With URLs post to see an example of some of the problems.

I knew that WordPress had a make_clickable function (in wp-includes/formatting.php) that did this exact thing. After testing this against some of the problems that Jeff points out it became clear that make_clickable() didn’t handle these edge cases. I made some rather crude tweaks to the WordPress code to fix some of these and opened ticket 8300 with my patches. Then filosofo came along and not only cleaned up my hacks, but reduced the amount of code needed in general. Major kudos to filosofo!

At this point it looks like we’ve got code to make make_clickable() work correctly with problem URLs. I’m going to wait until after WordPress 2.7 is released to push for getting this code committed since we’re trying to get 2.7 wrapped up.

I got thinking, this bit of code would be really handy to have as a stand alone library. So I pulled out the various pieces of code needed to make this work and put it together in a single PHP class: MakeItLink

HTML

<p>In late October Jeff Atwood wrote about <a href="http://www.codinghorror.com/blog/archives/001181.html">The Problems With URLs</a>, describing the problems of parsing out URLs in text and transforming them into links. Here&#x2019;s a simple example:</p> <blockquote><p> My website is at http://josephscott.org/ </p></blockquote> <p>Would be changed into:</p> <blockquote><p> My website is at &lt;a href=&#x2019;http://josephscott.org/&#x2019;&gt;http://josephscott.org/&lt;/a&gt; </p></blockquote> <p>Sounds simple right? Once you start looking at what the valid character set is for URLs things get tricky. I won&#x2019;t rehash all of items, go the <a href="http://www.codinghorror.com/blog/archives/001181.html">The Problem With URLs</a> post to see an example of some of the problems.</p> <p>I knew that <a href="http://wordpress.org/">WordPress</a> had a <a href="http://trac.wordpress.org/browser/tags/2.6.5/wp-includes/formatting.php#L685">make_clickable</a> function (in wp-includes/formatting.php) that did this exact thing. After testing this against some of the problems that Jeff points out it became clear that make_clickable() didn&#x2019;t handle these edge cases. I made some rather crude tweaks to the WordPress code to fix some of these and opened <a href="http://trac.wordpress.org/ticket/8300">ticket 8300</a> with my patches. Then filosofo came along and not only cleaned up my hacks, but reduced the amount of code needed in general. Major kudos to filosofo!</p> <p>At this point it looks like we&#x2019;ve got code to make make_clickable() work correctly with problem URLs. I&#x2019;m going to wait until after WordPress 2.7 is released to push for getting this code committed since we&#x2019;re trying to get 2.7 wrapped up.</p> <p>I got thinking, this bit of code would be really handy to have as a stand alone library. So I pulled out the various pieces of code needed to make this work and put it together in a single PHP class: MakeItLink</p> <div class="syntaxhighlighter" id="highlighter_120741"><div class="bar"><div class="toolbar"><a class="item viewSource" style="width: 16px; height: 16px;" title="view source" href="#viewSource">view source</a><div class="item copyToClipboard"><embed id="highlighter_120741_clipboard" type="application/x-shockwave-flash" title="copy to clipboard" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_120741" menu="false" src="http://josephscott.org/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" height="16" width="16"></div><a class="item printSource" style="width: 16px; height: 16px;" title="print" href="#printSource">print</a><a class="item about" style="width: 16px; height: 16px;" title="?" href="#about">?</a></div></div><div class="lines"><div class="line alt1"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px ! important;"><code class="keyword"></code></span></span></div></div></div>