<?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; search</title>
	<atom:link href="http://neverusethisfont.com/blog/tags/search/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>How to Let Google Power OpenSearch on Your Website</title>
		<link>http://neverusethisfont.com/blog/2011/07/how-to-let-google-power-opensearch-on-your-website/</link>
		<comments>http://neverusethisfont.com/blog/2011/07/how-to-let-google-power-opensearch-on-your-website/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 22:50:38 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Website Development]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[open search]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[site search]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://neverusethisfont.com/blog/?p=526</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><img src="http://neverusethisfont.com/blog/wp-content/uploads/2011/07/firefox-search-bar.png" alt="" title="firefox-search-bar" width="238" height="35" class="alignright size-full wp-image-527" /></p>
<p><a href="http://opensearch.org">OpenSearch</a> is a collection of simple formats for the sharing of search results. One of the formats allows your browser to add your site to its list of search engines, which Firefox displays in the top right corner, and Chrome uses in the address bar. Adding a small link and XML document allows your site to be added to a browser.</p>
<p>Many CMSs such as MediaWiki already support this natively so you might already have it on your site. To check in Chrome, in your search bar, enter your domain name followed by a space and see if it looks like the below screenshot.</p>
<p><img src="http://neverusethisfont.com/blog/wp-content/uploads/2011/07/chrome-search-bar.png" alt="" title="chrome-search-bar" width="471" height="82" class="aligncenter size-full wp-image-528" /></p>
<p>If not, you&#8217;ll have to add a small link and XML file.</p>
<p>The HTML code to reference the XML file is this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link href=&quot;/opensearch.xml&quot; rel=&quot;search&quot; title=&quot;aaronpk&quot; type=&quot;application/opensearchdescription+xml&quot;&gt;</pre></div></div>

<p>The XML file should look something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;OpenSearchDescription</span> <span style="color: #000066;">xmlns:moz</span>=<span style="color: #ff0000;">&quot;http://www.mozilla.org/2006/browser/search/&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://a9.com/-/spec/opensearch/1.1/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ShortName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>aaron.pk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ShortName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Search aaron.pk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;InputEncoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UTF-8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/InputEncoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Url</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;get&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;http://aaron.pk/search?q={searchTerms}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/OpenSearchDescription<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>But what if you want to add this to one of your sites that doesn&#8217;t have search capability? Turns out there is no restriction on the domains that you can use for the search URL. This means we can delegate the search to google!</p>
<p>Using Google&#8217;s &#8220;<code>site:example.com</code>&#8221; feature, we can restrict the Google search to our domain.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;OpenSearchDescription</span> <span style="color: #000066;">xmlns:moz</span>=<span style="color: #ff0000;">&quot;http://www.mozilla.org/2006/browser/search/&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://a9.com/-/spec/opensearch/1.1/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ShortName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>aaron.pk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ShortName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Search aaron.pk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;InputEncoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UTF-8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/InputEncoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Url</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;get&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;http://www.google.com/search?q={searchTerms}+site%3Aaaron.pk&amp;amp;hl=en&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/OpenSearchDescription<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then, if someone uses your site&#8217;s search, they get taken to a Google search results page with results from your site!</p>
<p><a href="http://neverusethisfont.com/blog/wp-content/uploads/2011/07/google-site-search.png"><img src="http://neverusethisfont.com/blog/wp-content/uploads/2011/07/google-site-search-570.png" alt="" title="google-site-search" width="570" height="271" class="aligncenter size-full wp-image-540" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://neverusethisfont.com/blog/2011/07/how-to-let-google-power-opensearch-on-your-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>URL Shortener as a Search Interface</title>
		<link>http://neverusethisfont.com/blog/2011/03/url-shortener-as-a-search-interface/</link>
		<comments>http://neverusethisfont.com/blog/2011/03/url-shortener-as-a-search-interface/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 18:41:44 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[cyborg anthropology]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[url shortener]]></category>

		<guid isPermaLink="false">http://neverusethisfont.com/blog/?p=431</guid>
		<description><![CDATA[cyborganthropology.com is quickly becoming the default online resource for everything relating to Cyborg Anthropology. With over 800 articles and growing daily, it&#8217;s possible to find nearly anything you can think of. We recently registered the domain &#8220;anth.ro&#8221; to use as a URL shortener to articles on cyborganthropology.com. I suggested creating a business card such as [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cyborganthropology.com">cyborganthropology.com</a> is quickly becoming the default online resource for everything relating to Cyborg Anthropology. With over 800 articles and growing daily, it&#8217;s possible to find nearly anything you can think of.</p>
<p>We recently registered the domain &#8220;<strong>anth.ro</strong>&#8221; to use as a URL shortener to articles on cyborganthropology.com. I suggested creating a business card such as this, which would allow Amber to write in arbitrary text following the URL after having a conversation with someone and giving them the card.</p>
<p><img src="http://neverusethisfont.com/blog/wp-content/uploads/2011/03/anthro-business-card.png" alt="" title="anthro-business-card" width="530" height="304" class="aligncenter size-full wp-image-445" /></p>
<p>Since she would obviously be offline while handing out cards, there would be no way to know for sure if a certain URL existed. The goal is to be able to write down any topic on the card and have the recipient be taken to the corresponding content when they get back to their computer.</p>
<h4>Site search</h4>
<p>If you didn&#8217;t already know, you can add &#8220;site:cyborganthropology.com&#8221; to the end of any search in Google to restrict your search to a specific website. Try it here: <a href="http://www.google.com/search?q=Second+self+site:cyborganthropology.com">google.com/search?q=Second+self+site:cyborganthropology.com</a>. This is a great start, because this lets us search the wiki for anything using Google&#8217;s excellent search algorithms and spelling correction.</p>
<h4>I&#8217;m Feeling Lucky</h4>
<p>Since we&#8217;ve restricted the search to this site, we can be reasonably confident that the first match is the one we intended. If we were to press Google&#8217;s &#8220;I&#8217;m feeling lucky&#8221; button, we&#8217;d probably get a good result. A quick Google search revealed that I can craft a URL which will automatically redirect to the first result, just like what happens when you press the button!</p>
<p>Adding &#8220;btnI=I&#8217;m+Feeling+Lucky&#8221; to the query string will automatically redirect the visitor.</p>
<p>Now our full search URL looks like this. Clicking this link will redirect you to the first page on the wiki matching &#8220;Second self&#8221;.</p>
<p><a href="http://www.google.com/search?q=Second+self+site:cyborganthropology.com&#038;btnI=I'm+Feeling+Lucky">http://www.google.com/search?q=Second+self+site:cyborganthropology.com&#038;btnI=I&#8217;m+Feeling+Lucky</a></p>
<h4>Short Domain</h4>
<p>Now let&#8217;s put that together with the short domain, <strong>anth.ro</strong>. We can easily set up a small script to redirect &#8220;http://anth.ro/second+self&#8221; to the full google search query. However, we&#8217;d ideally like to get some better Google analytics on how many times this URL shortener was used, so we&#8217;ll need to add some tracking parameters to the URL. Since we lose control of the visitor if we send them straight to Google, instead, we can query Google ourselves first to get the destination URL.</p>
<p>Here is a short PHP script which will do the following:</p>
<ul>
<li>Query Google with the &#8220;I&#8217;m feeling lucky&#8221; option to find the best match on our site</li>
<li>Add our additional tracking parameters</li>
<li>Redirect the browser to the page on the wiki with the tracking code</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.google.com/search?btnI=I\'m+Feeling+Lucky&amp;q='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'+site%3Acyborganthropology.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// Don't follow redirects, we want the first Location header</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// Return the headers in the output	curl_setopt($ch, CURLOPT_USERAGENT, &quot;Mozilla&quot;);     // Set the user agent to Mozilla so Google doesn't block the query</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_NOBODY<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// Don't include the response body in the output</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// Return the response in a variable instead of printing it out</span>
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// If there was a Location header found, use it, otherwise, Google couldn't find a match</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Location: (.+)/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// If the search result included query string parameters, parse those out</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'query'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// Add the search term as the &quot;q&quot; parameter. This is so Google Analytics will treat this as a &quot;site search&quot; query</span>
    <span style="color: #666666; font-style: italic;">// and include it in the search analytics. You'll also need to add &quot;q&quot; to the &quot;include site search&quot; section in Google Analytics</span>
    <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Add &quot;anth.ro&quot; as the utm_source</span>
    <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'utm_source'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'anth.ro'</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Add &quot;ShortURL&quot; as the medium. These end up appearing in the &quot;Traffic Sources&quot; list as &quot;anth.ro / ShortURL&quot;</span>
    <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'utm_medium'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ShortURL'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Redirect the user to the page specified by google with our additional parameters included</span>
    <span style="color: #000088;">$location</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://cyborganthropology.com'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'?'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$q</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTTP/1.1 302 Moved Temporarily'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$location</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// If there were no results returned from Google's I'm feeling lucky button, redirect to a Google search page</span>
<span style="color: #666666; font-style: italic;">// This most likely happens on a typo</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTTP/1.1 302 Moved Temporarily'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: http://www.google.com/search?q='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'+site%3Acyborganthropology.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Search</h4>
<p>With this in place, you can now use the <strong><a href="http://anth.ro">anth.ro</a></strong> domain as a search interface to the wiki! You can type in anything after anth.ro and you&#8217;ll either be taken to a page on the wiki or to a page of search results! We didn&#8217;t have to write any search code, we just let Google take care of that since that&#8217;s what they do best!</p>
]]></content:encoded>
			<wfw:commentRss>http://neverusethisfont.com/blog/2011/03/url-shortener-as-a-search-interface/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Filtering Array Elements in PHP Using an Anonymous Function</title>
		<link>http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/</link>
		<comments>http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 18:35:01 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Website Development]]></category>
		<category><![CDATA[anonymous functions]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/</guid>
		<description><![CDATA[Using an anonymous function in PHP's array_filter as a way to search a 2D array.]]></description>
			<content:encoded><![CDATA[<div style="text-align:center; background-color:#eee; border: 1px #bbb solid; padding: 4px;">== UPDATE ==<br />
This is now basically obsolete thanks to PHP 5.3&#8242;s <a href="http://www.php.net/manual/en/functions.anonymous.php">closures</a></div>
<p>I&#8217;ve used PHP&#8217;s <a href="http://php.net/array_filter">array_filter()</a> in the past to filter elements from an array, but occasionally I find that I need to pass an additional value to the function in order to determine whether to keep the element. This just came up in a case where I was using GET variables to filter search results on a large array.</p>
<p>I have a large 2D array of books where each record has some information such as title, author, publisher, ISBN, etc. I include search boxes for each column when I draw the table to the screen, so the user can search a specific column for some text.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Career and Life Planning&quot;</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">'Author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Michel Ozzi&quot;</span><span style="color: #339933;">,</span>      <span style="color: #0000ff;">'ISBN'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;978-0-07-284262-3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Beginning Algebra&quot;</span><span style="color: #339933;">,</span>         <span style="color: #0000ff;">'Author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Blitzer&quot;</span><span style="color: #339933;">,</span>          <span style="color: #0000ff;">'ISBN'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;978-0-555-03971-7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Primary Flight Briefing&quot;</span><span style="color: #339933;">,</span>   <span style="color: #0000ff;">'Author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Federal Aviation&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ISBN'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;978-1-5602755-7-2&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_GET</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$v</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$data'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return ( stripos($data[&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$k</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;], &quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$v</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;) !== false );'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Notice the values of <code lang="php">$k</code> and <code lang="php">$v</code> are actually written into the PHP code, whereas <code lang="php">$data</code> is written to the function as a variable name. If you were to echo the body of the function, it would actually look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;ISBN&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;555&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In each iteration of the loop the function is re-created, and the key/value pair are essentially hard-coded into the function instead of being variables.</p>
<p>So there you go! A real-live use for anonymous functions in PHP!</p>
]]></content:encoded>
			<wfw:commentRss>http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

