<?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>Gabriel de Kadt &#187; pendejo</title>
	<atom:link href="http://www.lazydada.com/tag/pendejo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lazydada.com</link>
	<description>Personal notes on Mac based web development and design.</description>
	<lastBuildDate>Tue, 03 Aug 2010 18:01:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>String to integer (strip all non numerics) with PHP</title>
		<link>http://www.lazydada.com/2009-06-19/string-to-integer-strip-all-non-numerics-with-php/</link>
		<comments>http://www.lazydada.com/2009-06-19/string-to-integer-strip-all-non-numerics-with-php/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 17:20:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web design & development]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[pendejo]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lazydada.com/?p=391</guid>
		<description><![CDATA[I&#8217;ve been hacking somebody else&#8217;s CMS (without access to the main engine) and needed to calculate a mathematical function on some prices. Specifically needed to display a percentage drop when given an original price as well as the current price. Here follows the journey. [Jump to The Solution.] Easy right? &#60;?php if($originalPrice &#62; 0) { [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been hacking somebody else&#8217;s CMS (without access to the main engine) and needed to calculate a mathematical function on some prices. Specifically needed to display a percentage drop when given an original price as well as the current price. Here follows the journey. <small>[Jump to <strong><a href="#solution">The Solution</a></strong>.]</small></p>
<p>Easy right?</p>
<pre>&lt;?php
if($originalPrice &gt; 0) {
	$percentageDrop = intval(100*(($originalPrice - $currentPrice)/$originalPrice));
}
?&gt;</pre>
<p>There was a Problem. The prices are stored not as integers but as formatted currency; ie &#8220;€&nbsp;120,000&#8243;</p>
<p>OK &#8211; easy &#8211; I found a simple solution to <a href="http://www.phpro.org/examples/Strip-All-Non-Alphanumeric-Characters-Except-Space-With-PHP.html">strip non alphanumerics (and space)</a> and adapted it slightly:</p>
<pre>&lt;?php
function numericFromString( $string )
{
	return preg_replace('/[^0-9]/', '', $string);
}
?&gt;</pre>
<p>This function is then applied like so:<br />
<code>$currentPrice = numericFromString( ' € 120,000 ' ) ;</code></p>
<p>Worked in principle but not in practice &#8211; numbers seemed to balloon in size. [I didn't spot it all the numbers which had '8364' at the beginning...]</p>
<p>I had to call in <a href="http://www.qdigital-imaging.com/index.php?call=gencontent&amp;s=8" title="PHP and database guru and all-round good chap">Dave &#8220;tech support&#8221; Watson</a>. &#8220;Bingo!&#8221; he said. </p>
<p>The problem  [you've figured it out already... ] is that the euro symbol is a numeric HTML entity: <code>&amp;#8364;</code>. There&#8217;s a bunch of numbers in there too. In fact &#8211; 8364.  </p>
<p><dfn title="Spanish profanity...meaning 'pubic hair'...translates more or less as 'jackass'... in Argentina, Chile and Uruguay... refers to a child, usually with a negative connotation, like referring to immaturity or brats."><em><a href="http://en.wikipedia.org/wiki/Spanish_profanity#Pendejo">Pendejo</a></em></dfn>.</p>
<p>So Dave took my adapted [and useless] function and made it work. Now it looks like this:</p>
<h3 id="solution" >The Solution</h3>
<pre>&lt;?php
function numericFromString($string) {
	$string = str_replace('&amp;#8364;','',$string);
	return preg_replace('/[^0-9]/','', html_entity_decode($string));
	}
?&gt;</pre>
<p>Why the extra line with <code>str_replace</code>? Because <code>html_entity_decode</code> doesn&#8217;t actually decode numerical html entities&#8230; </p>
<p>Would you believe it?</p>
<p>For more bullet proof results it may be worthwhile to try the following in place of that <code>str_replace</code> (from the comments at the official PHP manual at <a href="http://php.net/manual/en/function.html-entity-decode.php#47371">php.net</a>) </p>
<pre>
$string = preg_replace('/&#(\d+);/me',"chr(\\1)",$string); //decimal notation
$string = preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$string);  //hex notation
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lazydada.com/2009-06-19/string-to-integer-strip-all-non-numerics-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->