<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Never Use This Font &#187; regular expressions</title>
	<atom:link href="http://neverusethisfont.com/blog/tags/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://neverusethisfont.com/blog</link>
	<description>Aaron Parecki is the co-founder of Geoloqi.com, and specializes in backend systems development.</description>
	<lastBuildDate>Mon, 11 Jul 2011 22:50:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Automatically linking Twitter @usernames in PHP</title>
		<link>http://neverusethisfont.com/blog/2008/10/automatically-linking-twitter-usernames/</link>
		<comments>http://neverusethisfont.com/blog/2008/10/automatically-linking-twitter-usernames/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 18:30:41 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Website Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://neverusethisfont.com/blog/2008/10/automatically-linking-twitter-usernames/</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>I keep seeing people writing scripts that embed their Twitter feed into their websites. The &#8220;easy&#8221; way is to use Javascript, which means you don&#8217;t need to have PHP installed on your server. Doing it this way means your tweets will not be visible to visitors with Javascript disabled.</p>
<p>Really, nobody has Javascript disabled in their browsers anymore. The web is pretty much inaccessible without it at this point. However there are still some very important &#8220;visitors&#8221; that crawl around the web without javascript. I&#8217;m talking about search engines. Few if any search engines will actually execute Javascript on your site when crawling it for content. This means anything you have hidden inside a &lt;script&gt; tag will be hidden to them. If you want your tweets to be indexed as part of your page, then you&#8217;ll need to use PHP or another server-side scripting language to embed them into your page. This also has the other advantage of making your page load faster to regular visitors as well.</p>
<p>The below diagram should help illustrate the benefit of using PHP to embed your tweets.</p>
<p><img src="http://neverusethisfont.com/blog/wp-content/uploads/2008/10/twitter_php_rss_win.gif" alt="" align="center" /></p>
<p>Now that you&#8217;re convinced that you want to use PHP to embed your Twitter posts, you&#8217;re going to quickly run into the problem that people&#8217;s Twitter usernames are not given as a link in the RSS feed, but just the @username text. You probably want these usernames linked back to twitter.com.</p>
<p>I have seen some solutions involving splitting up the tweet into individual words, and looking at each to see if it begins with an @ sign. This involves a lot of code, and generally looks something like this:</p>
<p><img src="http://neverusethisfont.com/blog/wp-content/uploads/2008/10/twitter_garble.gif" alt="" align="center" /></p>
<p>It is rendered completely unnecessary by using one line of regular expressions!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(^|[^a-z0-9_])@([a-z0-9_]+)/i'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$1&lt;a href=&quot;http://twitter.com/$2&quot;&gt;@$2&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This regular expression is actually pretty simple. <strong>(updated)</strong> The key part is &#8220;(^|[^a-z0-9_])@([a-z0-9_]+)&#8221;, which is a lot less scary than it first looks. The ( ) are used to capture what&#8217;s inside them so that you can access it later (by using the $1 and $2 above). The [ ] match a set of characters, which can be defined as a range or a list of characters. We&#8217;re matching numbers and letters and the underscore. Finally, the + says &#8220;one or more&#8221;. The vertical bar | is used to match either what&#8217;s on the left <b>or</b> what&#8217;s on the right. The caret ^ (if it&#8217;s not inside square brackets) matches the beginning of a line.</p>
<p>So in English, this regular expression is looking for either the start of a line or a character other than a letter or number or underscore, followed by an @ sign, followed by one or more numbers or letters or the underscore, and storing those characters in the variable $2. This string is then replaced with the HTML code you see above, where $2 is set to the username by the regular expression.</p>
<p>Now that you understand the regular expression above, let me further complicate things by showing you how to make text that begins with http:// into a real hyperlink.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|(http://[^ ]+)|'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/(^|[^a-z0-9_])@([a-z0-9_]+)/i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;a href=&quot;$1&quot;&gt;$1&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$1&lt;a href=&quot;http://twitter.com/$2&quot;&gt;@$2&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replace</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Trust me, it isn&#8217;t that bad really. The new regular expression is actually simpler than the first, but is looking for http:// instead of @. You may have also noticed that I switched from using // to ||. You can use any character as the bounds for the regular expression. The advantage of using | is that the bar doesn&#8217;t appear inside ever. If I used / as the bounds, then had http:// inside, I&#8217;d have to escape the forward slashes of the http. (It would look like http:\/\/, which is kind of ridiculous).</p>
<p>You might want to check out <a href="http://www.regular-expressions.info">http://www.regular-expressions.info</a> to learn more about regular expressions. Regular expressions are an extremely powerful tool you will want to add to your arsenal when learning PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://neverusethisfont.com/blog/2008/10/automatically-linking-twitter-usernames/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		</item>
	</channel>
</rss>

