<?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>willoller.com &#187; php</title>
	<atom:link href="http://www.willoller.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.willoller.com</link>
	<description>Learning to match the beat of the Old World man.</description>
	<lastBuildDate>Thu, 02 Feb 2012 23:34:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Ordinal Suffixes in PHP</title>
		<link>http://www.willoller.com/ordinal-suffixes-in-php/</link>
		<comments>http://www.willoller.com/ordinal-suffixes-in-php/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 21:23:55 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.willoller.com/?p=246</guid>
		<description><![CDATA[function ordinalize&#40;$int&#41; &#123; &#160; &#160;//sanity &#160; &#160;$int = &#40;int&#41;$int; &#160; &#160;if&#40;!is_numeric&#40;$int&#41;&#41; return false; &#160; &#160;// You could internationalize here &#160; &#160;$suffixes = array&#40;'th', 'st', 'nd', 'rd'&#41;; &#160; &#160;// Get the number in the tens place &#160; &#160;$tens = substr&#40;$int,-2,1&#41;; &#160; &#160;// All of the numbers 10-19 return 'th' &#160; &#160;if&#40;$tens == 1&#41; return $int.$suffixes&#91;0&#93;; &#160; [...]]]></description>
			<content:encoded><![CDATA[	<div class="code php" style="font-family: monospace;"><br />
<span style="color: #000000; font-weight: bold;">function</span> ordinalize<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">//sanity</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$int</span> = <span style="color: #66cc66;">&#40;</span>int<span style="color: #66cc66;">&#41;</span><span style="color: #0000ff;">$int</span>;<br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<a href="http://www.php.net/is_numeric"><span style="color: #000066;">is_numeric</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// You could internationalize here</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$suffixes</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'th'</span>, <span style="color: #ff0000;">'st'</span>, <span style="color: #ff0000;">'nd'</span>, <span style="color: #ff0000;">'rd'</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// Get the number in the tens place</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$tens</span> = <a href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$int</span>,<span style="color: #cc66cc;">-2</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// All of the numbers 10-19 return 'th'</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tens</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$int</span>.<span style="color: #0000ff;">$suffixes</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// Get the number in the ones place</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$cardinal</span> = <a href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$int</span>,<span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// All numbers ending in 1, 2, 3 have special suffixes</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$suffixes</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$cardinal</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$int</span>.<span style="color: #0000ff;">$suffixes</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$cardinal</span><span style="color: #66cc66;">&#93;</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// All remaining numbers end in 'th'</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$int</span>.<span style="color: #0000ff;">$suffixes</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;<br />
<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp;</div>

 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/ordinal-suffixes-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Textile ol li count bug</title>
		<link>http://www.willoller.com/textile-ol-li-count-bug/</link>
		<comments>http://www.willoller.com/textile-ol-li-count-bug/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 20:10:41 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.willoller.com/?p=201</guid>
		<description><![CDATA[Just hit a bug with the Textile 2.0 library: # Portland, OR # Sanford, FL # Albuquerque, NM # San Diego, CA # Bellingham, WA &#160; Was producing: &#60;ol&#62; &#160; &#160;&#60;li&#62;Portland, OR&#60;/li&#62; &#160; &#160;&#60;li&#62;Sanford, FL&#60;/li&#62; &#160; &#160;&#60;li&#62;Albuquerque, NM&#60;/li&#62; &#160; &#160;&#60;li&#62;San Diego, CA&#160; &#160;&#60;/li&#62; &#60;/ol&#62; &#60;ol&#62; &#160; &#160;&#60;li&#62;Bellingham, WA&#60;/li&#62; &#60;/ol&#62; &#160; Found the solution here: You [...]]]></description>
			<content:encoded><![CDATA[	<p>Just hit a bug with the Textile 2.0 library:</p>
	<div class="code html" style="font-family: monospace;"><br />
# Portland, OR<br />
# Sanford, FL<br />
# Albuquerque, NM<br />
# San Diego, CA<br />
# Bellingham, WA<br />
&nbsp;</div>
	<p>Was producing:</p>
	<div class="code html" style="font-family: monospace;"><br />
&lt;ol&gt;<br />
&nbsp; &nbsp;&lt;li&gt;Portland, OR&lt;/li&gt;<br />
&nbsp; &nbsp;&lt;li&gt;Sanford, FL&lt;/li&gt;<br />
&nbsp; &nbsp;&lt;li&gt;Albuquerque, NM&lt;/li&gt;<br />
&nbsp; &nbsp;&lt;li&gt;San Diego, CA&nbsp; &nbsp;&lt;/li&gt;<br />
&lt;/ol&gt;<br />
&lt;ol&gt;<br />
&nbsp; &nbsp;&lt;li&gt;Bellingham, WA&lt;/li&gt;<br />
&lt;/ol&gt;<br />
&nbsp;</div>
	<p>Found the solution <a href="http://forum.textpattern.com/viewtopic.php?id=24701&#38;p=3">here</a>:</p>
<blockquote>You can work around the problem, by replacing these two lines (in the fList function in /textpattern/lib/classTextile.php):
<div class="code php" style="font-family: monospace;"><br />
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$text</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$line</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">$nextline</span> = <a href="http://www.php.net/next"><span style="color: #000066;">next</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$text</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp;</div>
with:
<div class="code php" style="font-family: monospace;"><br />
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$text</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$nr</span> =&gt; <span style="color: #0000ff;">$line</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">$nextline</span> = <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$text</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$nr</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #0000ff;">$text</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$nr</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span> : <span style="color: #000000; font-weight: bold;">false</span>;<br />
<br />
&nbsp;</div>
</blockquote>

 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/textile-ol-li-count-bug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Counting Characters php vs javascript</title>
		<link>http://www.willoller.com/counting-characters-php-vs-javascript/</link>
		<comments>http://www.willoller.com/counting-characters-php-vs-javascript/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 04:13:05 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[php programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.willoller.com/?p=159</guid>
		<description><![CDATA[On a recent project, an issue cropped up where we were exporting xml reports to a third party, who had rules about line lengths in Description fields. So, I wrote up a quick-and-dirty character counter for the description field so the client can keep their descriptions under the 1500-word limit. Here&#8217;s how it was done: [...]]]></description>
			<content:encoded><![CDATA[	<p>On a recent project, an issue cropped up where we were exporting xml reports to a third party, who had rules about line lengths in Description fields.</p>
	<p>So, I wrote up a quick-and-dirty character counter for the description field so the client can keep their descriptions under the 1500-word limit.</p>
	<p>Here&#8217;s how it was done:</p>
	<div class="code php" style="font-family: monospace;"><br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;You must enter a Description&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">1500</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$len</span> = <a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;Descriptions are limited to 1500 characters ($len)&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp;</div>
	<p>Now the html and javascript:</p>
	<div class="code html" style="font-family: monospace;"><br />
&lt;li class=&quot;description&quot;&gt;<br />
&nbsp; &nbsp;&lt;label for=&quot;htmlarea&quot; class=&quot;required&quot;&gt;Description *&lt;/label&gt;<br />
&nbsp; &nbsp;&lt;textarea name=&quot;description&quot; id=&quot;htmlarea&quot; cols=&quot;80&quot; rows=&quot;20&quot; class=&quot;textarea&quot;&gt;&lt; ?= $ff['description'] ?&gt;&lt;/textarea&gt;<br />
&nbsp; &nbsp;&lt;p class=&quot;hint&quot; id=&quot;desc_char_count&quot;&gt;1500 character limit&lt;/p&gt;<br />
&lt;/li&gt;<br />
&nbsp;</div>
	<div class="code javascript" style="font-family: monospace;"><br />
$<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#htmlarea'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">keyup</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> charLength = $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span>;<br />
&nbsp; &nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#desc_char_count'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span>charLength + <span style="color: #3366CC;">' of 1500 characters used'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span> &gt; <span style="color: #CC0000;">1500</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#desc_char_count'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'positive'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'negative'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;strong&gt;You have '</span> + $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span> + <span style="color: #3366CC;">' characters. You may only have up to 1500 characters.&lt;/strong&gt;'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#desc_char_count'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'positive'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'negative'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#htmlarea'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">keyup</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp;</div>
	<p>All was working well, except I underestimated the descriptions. The next morning, the thing was broken. The php side was kicking validation errors (&#8220;over 1500 characters&#8221;), while the javascript was counting a mere 1480. Why?</p>
	<p>Turns out the data was full of bullets and other multibyte characters. To make the counts match, I needed multibyte-safe php:</p>
	<div class="code php" style="font-family: monospace;"><br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;You must enter a Description&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/mb_strlen"><span style="color: #000066;">mb_strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #ff0000;">'UTF-8'</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">1500</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$len</span> = <a href="http://www.php.net/mb_strlen"><span style="color: #000066;">mb_strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #ff0000;">'UTF-8'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;Descriptions are limited to 1500 characters ($len)&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp;</div>
	<p>Hooray! Now the counts match!</p>

 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/counting-characters-php-vs-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

