<?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>dan.thoughts &#187; Web Development</title>
	<atom:link href="http://blog.sosedoff.com/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sosedoff.com</link>
	<description>Web-development, PHP, Ruby, Sinatra, Merb, Rails, MySQL, SQLite, Web Services.</description>
	<lastBuildDate>Sat, 03 Jul 2010 05:46:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Debugging PHP applications in terminal</title>
		<link>http://blog.sosedoff.com/2010/07/03/debugging-php-applications-in-terminal/</link>
		<comments>http://blog.sosedoff.com/2010/07/03/debugging-php-applications-in-terminal/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 05:45:39 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[eventmachine]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=251</guid>
		<description><![CDATA[Most of Ruby web frameworks have terminal logging in development environments. It makes application debugging process much easier than using file logging. Especially for AJAX requests. Of course there is simple solution &#8211; use Firebug and javascript console. Unfortunately it is not that convenient. 
So, one day i came up with idea to make the [...]]]></description>
			<content:encoded><![CDATA[<p>Most of Ruby web frameworks have terminal logging in development environments. It makes application debugging process much easier than using file logging. Especially for AJAX requests. Of course there is simple solution &#8211; use Firebug and javascript console. Unfortunately it is not that convenient. </p>
<p>So, one day i came up with idea to make the same ruby-like style of application logging, even with colorized output.<br />
I decided to use <a href="http://eventmachine.rubyforge.org/">EventMachine</a> library as a server platform. And API is based on JSON packets, via <a href="http://flori.github.com/json/">json/pure</a>. Simple, isnt it?</p>
<p>And i called it DebugServer. Pretty understandable <img src='http://blog.sosedoff.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Installation</h3>
<p>To install just type</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> debugserver</pre></div></div>

<p>And for information type:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ debugserver <span style="color: #660033;">-i</span></pre></div></div>

<pre>
Usage: debugserver [options]
    -h, --host HOSTNAME              Server hostname
    -p, --port PORT                  Server port
    -i, --info                       Get usage information
</pre>
<p>By default it will start on localhost:9000. </p>
<h3>Usage in PHP</h3>
<p>First, download library from GitHub repository: <a href="http://github.com/sosedoff/debugclient-php">http://github.com/sosedoff/debugclient-php</a></p>
<p>And then:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// place it into app initialization file</span>
<span style="color: #000088;">$debug</span> <span style="color: #339933;">=</span> DebugClient<span style="color: #339933;">::</span><span style="color: #004000;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$debug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// .... and write it to terminal output</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xFFFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sample name'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'time'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%m-%d-%Y'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// these functions are globally defined</span>
debug_clear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// clear terminal</span>
debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This is a plain text message.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug_info<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This is an informational message.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug_warning<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This is a warning message.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This is an error message.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug_dump<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// optional</span>
<span style="color: #000088;">$debug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Results:<br />
<img src="http://blog.sosedoff.com/wp-content/uploads/2010/07/debugserver.png" alt="Output" /></p>
<h3>Sources</h3>
<p>DebugServer: <a href="http://github.com/sosedoff/debugserver">http://github.com/sosedoff/debugserver</a><br />
PHP Client: <a href="http://github.com/sosedoff/debugclient-php">http://github.com/sosedoff/debugclient-php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/07/03/debugging-php-applications-in-terminal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generate sitemaps with Ruby and XmlSitemap gem</title>
		<link>http://blog.sosedoff.com/2010/06/18/generate-sitemaps-with-ruby-and-xmlsitemap-gem/</link>
		<comments>http://blog.sosedoff.com/2010/06/18/generate-sitemaps-with-ruby-and-xmlsitemap-gem/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 23:10:38 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sinatra]]></category>
		<category><![CDATA[sitemap]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=244</guid>
		<description><![CDATA[Made a simple gem for website sitemap generation. Could be used in any Ruby/Rails/Merb/Sinatra application. It does not have any caching in that case if you want to use framework built-in cache methods. 
Installation:

$ sudo gem install xml-sitemap

Example

pages = Page.all&#40;:order =&#62; &#91;:updated_at.desc&#93; # DM model
map = XmlSitemap::Map.new&#40;'somedomain.com'&#41; do &#124;m&#124;
  m.add&#40;:url =&#62; '/', :period =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Made a simple gem for website sitemap generation. Could be used in any Ruby/Rails/Merb/Sinatra application. It does not have any caching in that case if you want to use framework built-in cache methods. </p>
<p>Installation:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> xml-sitemap</pre></div></div>

<h3>Example</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">pages = Page.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:updated_at</span>.<span style="color:#9900CC;">desc</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;"># DM model</span>
map = <span style="color:#6666ff; font-weight:bold;">XmlSitemap::Map</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'somedomain.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span>
  m.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'/'</span>, <span style="color:#ff3333; font-weight:bold;">:period</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:daily</span>, <span style="color:#ff3333; font-weight:bold;">:priority</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1.0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  m.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'/contractors'</span>, <span style="color:#ff3333; font-weight:bold;">:period</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:daily</span>, <span style="color:#ff3333; font-weight:bold;">:priority</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1.0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  pages.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span>
    m.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>
      <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> url_for_page<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:updated</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">updated_at</span>,
      <span style="color:#ff3333; font-weight:bold;">:priority</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0.5</span>,
      <span style="color:#ff3333; font-weight:bold;">:period</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:never</span>
    <span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># render the sitemap</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> map.<span style="color:#9900CC;">render</span></pre></div></div>

<h3>Sinatra Example</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># ... your code</span>
&nbsp;
get <span style="color:#996600;">'/sitemap.xml'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  map = <span style="color:#6666ff; font-weight:bold;">XmlSitemap::Map</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'domain.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span>
    m.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'/'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    m.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'/posts'</span>, <span style="color:#ff3333; font-weight:bold;">:period</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:weekly</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  headers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Content-Type'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'text/xml'</span>
  map.<span style="color:#9900CC;">render</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># ... more code</span></pre></div></div>

<h3>Options</h3>
<p>:url &#8211; page path, relative to domain (ex.: /test), String.<br />
:period &#8211; freqchange attribute, Symbol, :none, :never, :always, :hourly, :daily, :weekly, :monthly, :yearly<br />
:priority &#8211; priority attribute, Float class,(0.0..1.0)<br />
:updated &#8211; (optional) last_update attribute, Time class</p>
<h3>Source Code</h3>
<p><a href="http://github.com/sosedoff/xml-sitemap">http://github.com/sosedoff/xml-sitemap</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/06/18/generate-sitemaps-with-ruby-and-xmlsitemap-gem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making HTTP requests from different network interfaces with Ruby and Curb</title>
		<link>http://blog.sosedoff.com/2010/06/09/making-http-requests-from-different-network-interfaces-with-ruby-and-curb/</link>
		<comments>http://blog.sosedoff.com/2010/06/09/making-http-requests-from-different-network-interfaces-with-ruby-and-curb/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 04:31:17 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[curb]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[net]]></category>
		<category><![CDATA[requests]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=228</guid>
		<description><![CDATA[At some point you will find that you have reached requests per IP limit while using some API or crawling resources. And if you`re doing it via standard Net::HTTP you`ll face the problem that you cannot assign request class to specified network interface (or IP). Bummer? No. Even if you cant do it with core [...]]]></description>
			<content:encoded><![CDATA[<p>At some point you will find that you have reached requests per IP limit while using some API or crawling resources. And if you`re doing it via standard <a href="http://ruby-doc.org/core/classes/Net/HTTP.html">Net::HTTP</a> you`ll face the problem that you cannot assign request class to specified network interface (or IP). Bummer? No. Even if you cant do it with core class you might take a look on <a href="http://curb.rubyforge.org/">Curb</a> &#8211; libcurl ruby binding. It has everything that you need to make regular get/post/etc requests. And of course &#8211; easy.</p>
<p>A simple example (real ip`s are changed):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'curb'</span>
&nbsp;
ip_addresses = <span style="color:#006600; font-weight:bold;">&#91;</span>
  <span style="color:#996600;">'1.1.1.1'</span>,
  <span style="color:#996600;">'2.2.2.2'</span>,
  <span style="color:#996600;">'3.3.3.3'</span>,
  <span style="color:#996600;">'4.4.4.4'</span>,
  <span style="color:#996600;">'5.5.5.5'</span>
<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
ip_addresses.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ip<span style="color:#006600; font-weight:bold;">|</span>
  req = <span style="color:#6666ff; font-weight:bold;">Curl::Easy</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://www.ip-adress.com/'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  req.<span style="color:#9900CC;">interface</span> = ip
  req.<span style="color:#9900CC;">perform</span>
  result_ip = req.<span style="color:#9900CC;">body_str</span>.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/&lt;</span>h2<span style="color:#006600; font-weight:bold;">&gt;</span>My IP address is: <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>\d\.<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">1</span>,<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;</span>\<span style="color:#006600; font-weight:bold;">/</span>h2<span style="color:#006600; font-weight:bold;">&gt;/</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;for #{ip} got response: #{result_ip}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Output (ip`s are changed):</p>
<pre>
for 1.1.1.1 got response: 1.1.1.1
for 2.2.2.2 got response: 2.2.2.2
for 3.3.3.3 got response: 3.3.3.3
for 4.4.4.4 got response: 4.4.4.4
for 5.5.5.5 got response: 5.5.5.5
</pre>
<p>At least its working. Havent done any performance tests.<br />
Sample on pastie: <a href="http://pastie.org/private/afxlcuk1npwjov3wer5hw">http://pastie.org/private/afxlcuk1npwjov3wer5hw</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/06/09/making-http-requests-from-different-network-interfaces-with-ruby-and-curb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to identify your clients` browser type</title>
		<link>http://blog.sosedoff.com/2009/10/02/how-to-identify-your-clients-browser-type/</link>
		<comments>http://blog.sosedoff.com/2009/10/02/how-to-identify-your-clients-browser-type/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 18:44:40 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[agent]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=178</guid>
		<description><![CDATA[Some time ago i wrote a simple regex patterns to determine whether my client crawler bot, mobile client or just regular one. Easy to expand and to use.

function is_mobile&#40;$agent&#41; &#123;
    $pattern = '/(blackberry&#124;motorokr&#124;motorola&#124;sony&#124;windows ce&#124;240x320&#124;176x220&#124;palm&#124;mobile&#124;iphone&#124;ipod&#124;symbian&#124;nokia&#124;samsung&#124;midp)/i';
    return &#40;bool&#41;preg_match&#40;$pattern, $agent&#41;;
&#125;
&#160;
function is_crawler&#40;$agent&#41; &#123;
    pattern = '/(google&#124;yahoo&#124;baidu&#124;bot&#124;webalta&#124;ia_archiver)/';
    return [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago i wrote a simple regex patterns to determine whether my client crawler bot, mobile client or just regular one. Easy to expand and to use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_mobile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$agent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/(blackberry|motorokr|motorola|sony|windows ce|240x320|176x220|palm|mobile|iphone|ipod|symbian|nokia|samsung|midp)/i'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$agent</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;">function</span> is_crawler<span style="color: #009900;">&#40;</span><span style="color: #000088;">$agent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    pattern <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/(google|yahoo|baidu|bot|webalta|ia_archiver)/'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$agent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/10/02/how-to-identify-your-clients-browser-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails-like PHP url router</title>
		<link>http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/</link>
		<comments>http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 16:33:03 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[rails-like]]></category>
		<category><![CDATA[routes]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=167</guid>
		<description><![CDATA[My previous version of php url router was not good enough, so i`ve done some core modification to the class. Its more flexible now. The whole idea is very similar to Rails` and Merb`s url router (merb is a rails-fork). In fact, previous version doesn`t even support GET variables in the request uri. New version [...]]]></description>
			<content:encoded><![CDATA[<p>My previous version of php url router was not good enough, so i`ve done some core modification to the class. Its more flexible now. The whole idea is very similar to Rails` and Merb`s url router (merb is a rails-fork). In fact, previous version doesn`t even support GET variables in the request uri. New version just merges variables from request string into the same parameters array where other variables are stored. Also there is a command &#8220;default_routes&#8221; that maps default routes like this &#8220;/:controller/:action/:id&#8221;. So, basically you have two options &#8211; use defaults &#038; custom routes or use only custom, which means that all other requests will be ignored. Function default_routes must be declared last. </p>
<p>Here is the sources:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROUTER_DEFAULT_CONTROLLER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'home'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROUTER_DEFAULT_ACTION'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Router <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$request_uri</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$routes</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">,</span> <span style="color: #000088;">$controller_name</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$action</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$params</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$route_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request_uri</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">routes</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: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> map<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</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: #000088;">$conditions</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">routes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Route<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request_uri</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> default_routes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/:controller'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/:controller/:action'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/:controller/:action/:id'</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;">private</span> <span style="color: #000000; font-weight: bold;">function</span> set_route<span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">route_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span> <span style="color: #339933;">=</span> ROUTER_DEFAULT_CONTROLLER<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span> <span style="color: #339933;">=</span> ROUTER_DEFAULT_ACTION<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$w</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</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: #000088;">$w</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">routes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$route</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_matched</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_route</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Route <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$is_matched</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$params</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request_uri</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conditions</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$p_names</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: #000088;">$p_values</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>
&nbsp;
    <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@:([\w]+)@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p_names</span><span style="color: #339933;">,</span> PREG_PATTERN_ORDER<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$p_names</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$p_names</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$url_regex</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@:[\w]+@'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'regex_url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$url_regex</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/?'</span><span style="color: #339933;">;</span>
&nbsp;
    <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;">'@^'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url_regex</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'$@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request_uri</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p_values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_names</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_values</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_matched</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_names</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_values</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;">function</span> regex_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conditions</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'('</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conditions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</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: #009900;">&#125;</span> 
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'([a-zA-Z0-9_\+\-%]+)'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Setup example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Router<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// create router instance </span>
&nbsp;
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'home'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// main page will call controller &quot;Home&quot; with method &quot;index()&quot;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/login'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'auth'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/logout'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'auth'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'logout'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/signup'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'auth'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'signup'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/profile/:action'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'profile'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will call controller &quot;Profile&quot; with dynamic method &quot;:action()&quot;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/:id'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'[\d]{1,8}'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// define filters for the url parameters</span>
&nbsp;
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">default_routes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Usage example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$router</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Router<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ... some configs ...</span>
<span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will return name as it appears in url, ex: 'user_images'</span>
<span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller_name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will return processed name of controller</span>
<span style="color: #666666; font-style: italic;">// for example, if class name in url is 'user_images', then 'controller_name' var will be UserImages</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// if parameter :id presents</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// array(...)</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">route_matched</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// true - if route found, false - if not</span></pre></div></div>

<p>Small and useful class. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Identify www bots and mobile devices</title>
		<link>http://blog.sosedoff.com/2009/03/12/identify-www-bots-and-mobile-devices/</link>
		<comments>http://blog.sosedoff.com/2009/03/12/identify-www-bots-and-mobile-devices/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 08:45:50 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[crawler]]></category>
		<category><![CDATA[device]]></category>
		<category><![CDATA[identify]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[user agent]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=79</guid>
		<description><![CDATA[Need to identify web crawlers and mobile devices with your web-app ? Here is the list of couple regular expressions you can use (most common values):
Mobile Devices:

/(blackberry&#124;motorokr&#124;motorola&#124;sony&#124;windows ce&#124;240x320&#124;176x220&#124;palm&#124;mobile&#124;iphone&#124;ipod&#124;symbian&#124;nokia&#124;samsung&#124;midp)/i

Web Spyders:

/(google&#124;yahoo&#124;baidu&#124;bot&#124;webalta&#124;archiver&#124;crawler&#124;spyder)/i

]]></description>
			<content:encoded><![CDATA[<p>Need to identify web crawlers and mobile devices with your web-app ? Here is the list of couple regular expressions you can use (most common values):</p>
<p>Mobile Devices:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/(blackberry|motorokr|motorola|sony|windows ce|240x320|176x220|palm|mobile|iphone|ipod|symbian|nokia|samsung|midp)/i</pre></div></div>

<p>Web Spyders:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/(google|yahoo|baidu|bot|webalta|archiver|crawler|spyder)/i</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/03/12/identify-www-bots-and-mobile-devices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
