<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Aaha Creative: Web Developer</title>
	
	<link>http://aahacreative.com</link>
	<description>How long before you say AaHa?</description>
	<lastBuildDate>Mon, 27 Feb 2012 13:33:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/AahaCreative" /><feedburner:info uri="aahacreative" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Dynamically Resizing Text with jQuery</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/Te1K_1_GEWg/</link>
		<comments>http://aahacreative.com/2012/02/27/dynamically-resizing-text-jquery/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 12:50:25 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=631</guid>
		<description><![CDATA[Dynamically resized text ensures that when necessary, text can fit to a pixel-perfect width in any browser and any operating system. While fonts are similar enough across most browsers and OS, in headlines or other locations where overflow text would break layout, occasionally, one browser will have issues where otheres don&#8217;t. In this case, we [...]]]></description>
			<content:encoded><![CDATA[<p>Dynamically resized text ensures that when necessary, text can fit to a pixel-perfect width in any browser and any operating system. While fonts are similar enough across most browsers and OS, in headlines or other locations where overflow text would break layout, occasionally, one browser will have issues where otheres don&#8217;t. In this case, we can ensure that all browsers work at once with a little jQuery.</p>

<span class="post_thumbnail "><img width="586" height="150" src="http://aaha.s3.amazonaws.com/files/2012/02/dynamically-resized-text.png" class="attachment-post-hero wp-post-image" alt="dynamically resized text" title="dynamically resized text" /></span>

<p><a href="http://aahacreative.com/demos/dynamically-resize-text-js/">See the Demo </a></p>

<p>The demo is made of three snippets:</p>

<p>The HTML:
<pre class="brush: html">&lt;div id=&quot;demo&quot;&gt;
&lt;p&gt;All of these lines will have different font sizes.&lt;/p&gt;
&lt;p&gt;This is because we have set the white-space property of the p tags to &quot;pre.&quot;&lt;/p&gt;
&lt;p&gt;This means that these lines won&#039;t wrap, and because they won&#039;t ever wrap, when the page loads,&lt;/p&gt;
&lt;p&gt;The JS will attempt to fit the spans inside the p tags. Of course, this could be done with p tags inside of divs.&lt;/p&gt;
&lt;/div&gt;</pre></p>

<p>The CSS:</p>

<p><pre class="brush: css">#demo p{
white-space:pre;
}
#demo {
width:600px;
}</pre></p>

<p>And the JS magic that fits a span into every <abbr>p</abbr> tag and then scales the font by percents.</p>

<p><pre class="brush: js">&lt;script type=&quot;text/javascript&quot;&gt;
jQuery(&quot;#demo p&quot;)
    .contents()
    .filter(
        function(){
            return this.nodeType != 1; 
        })
    .wrap(&quot;&lt;span/&gt;&quot;).parent().each(
	function(){
		var size = 100;
		while(jQuery(this).parent().width() &lt; jQuery(this).width() &amp;&amp; size &gt; 5){
			size -= 1;
			jQuery(this).css({&#039;font-size&#039; : size+&#039;%&#039;});
		}
	}
);

&lt;/script&gt;</pre></p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/Te1K_1_GEWg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2012/02/27/dynamically-resizing-text-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2012/02/27/dynamically-resizing-text-jquery/</feedburner:origLink></item>
		<item>
		<title>Executing inline JavaScript when using AJAX</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/v8GQ2k_vmhI/</link>
		<comments>http://aahacreative.com/2011/11/27/executing-inline-javascript-ajax/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 12:11:38 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=258</guid>
		<description><![CDATA[AJAX is full of stumbling blocks. From browser support to user expectations, it can cause more problems than it solves when used at the CMS level. In AJAXed WordPress, I had to find a way to enable javascript processing on arbitrary scripts from other plugins loaded with AJAX. Typically, this is impossible because most inline [...]]]></description>
			<content:encoded><![CDATA[<p>AJAX is full of stumbling blocks. From browser support to user expectations, it can cause more problems than it solves when used at the CMS level. In <a href="http://ajaxedwp.com">AJAXed WordPress</a>, I had to find a way to enable javascript processing on arbitrary scripts from other plugins loaded with AJAX. Typically, this is impossible because most inline JS uses the <a href="http://www.mediacollege.com/internet/javascript/basic/document-write.html">document.write</a> function to output content. Once the page is written with the updated content, <span class="inline-code">document.write</span> is dangerous and can overwrite the entire page.</p>

<span class="post_thumbnail "><img width="590" height="150" src="http://aahacreative.com/files/2011/11/javascript-document1.png" class="attachment-post-hero wp-post-image" alt="javascript document" title="javascript document" /></span>

<p>Even more importantly, inline JS is never run when embedded &lt;script> tags are used with the innerHTML command. The following JavaScript function fixes both problems by eval&#8217;ing the content of the &lt;script> tags and replacing the default document.write with a new function that replaces the inline JS with the output of the document.write function.</p>

<h3> The Code</h3>

<p><pre class="brush: javascript">function executeJS(i){ // Pass an element ID to the function.

	var e = document.getElementById(i); 
	var Reg = &#039;(?:&lt;script.*?&gt;)((\n|.)*?)(?:&lt;/script&gt;)&#039;; //Regex for all content between &lt;script&gt; tags.
	var match    = new RegExp(Reg, &#039;img&#039;);
	var scripts  = e.innerHTML.match(match);

	var doc = document.write; // Store the functionality of document.write temporarily.
	//Overwrite document.write with a new function that takes the innerHTML of the passed element
	//then replaces the &lt;script&gt; tag with the output of the passed content.
	document.write = function(p){ e.innerHTML = e.innerHTML.replace(scripts[s],p)};

	if(scripts) { //if matches
		for(var s = 0; s &lt; scripts.length; s++) { //loop through matches
			var js = &#039;&#039;;
			var match = new RegExp(Reg, &#039;im&#039;); //find first match
			js = scripts[s].match(match)[1]; //inner content of the first match
			eval(&#039;try{&#039;+js+&#039;}catch(e){}&#039;); //evaluate the inline JS.
		}

	}

	document.write = doc; //Reset document.write back to its original functionality

}</pre></p>

<p>This example function only outputs the first document.write in a script block, so if you require more than one in your embedded JS, it is trivial to store the output content as a variable.</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/v8GQ2k_vmhI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2011/11/27/executing-inline-javascript-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2011/11/27/executing-inline-javascript-ajax/</feedburner:origLink></item>
		<item>
		<title>Stripslashes all values in an array with PHP.</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/l0hKhrAw5f0/</link>
		<comments>http://aahacreative.com/2010/07/26/stripslashes-all-values-in-an-array-with-php/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 13:27:28 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=256</guid>
		<description><![CDATA[Here is a very simple function to run stripslashes on an array and all of its child arrays. The function below is for PHP 5 and takes a single argument which should consist of an array. It loops through all elements of the array, and if an element is an array, it calls itself on [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a very simple function to run stripslashes on an array and all of its child arrays. The function below is for PHP 5 and takes a single argument which should consist of an array. It loops through all elements of the array, and if an element is an array, it calls itself on the child array to an infinite depth. Of course this can cause memory problems if your arrays are dynamically created with an excessively large depth, but for most uses, it will function perfectly.</p>

<h3>The Code</h3>

<p><pre class="brush: php">function unstrip_array($array){
	foreach($array as &amp;$val){
		if(is_array($val)){
			$val = unstrip_array($val);
		}else{
			$val = stripslashes($val);
		}
	}
return $array;
}</pre></p>

<h3>Why would I need to unstrip an entire array?</h3>

<p>It&#8217;s useful for WordPress Plugin developers or anyone whose scripts might be run on a server with magic quotes enabled. It is also excellent to strip any slashes from <span class="inline-code">$_POST</span> inputs that might confuse your attempts to escape the strings later. Running an array of post data through this will make it easier to ensure that you don&#8217;t have hacking attempts.</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/l0hKhrAw5f0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2010/07/26/stripslashes-all-values-in-an-array-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2010/07/26/stripslashes-all-values-in-an-array-with-php/</feedburner:origLink></item>
		<item>
		<title>Another look at jQuery Performance: combining elements and IDs</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/x3hXwuqMu9s/</link>
		<comments>http://aahacreative.com/2010/07/23/jquery-performance-combining-elements-ids/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 15:57:03 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=595</guid>
		<description><![CDATA[A question that came up in regards to jQuery Performance was: is it faster to define the type of element before you set the ID. Basically, is jQuery(&#039;textarea#content_box&#039;) better than jQuery(&#039;#content_box&#039;)? The thinking is that because the latter is more specific, the parser has to do less work to find the element. But this is [...]]]></description>
			<content:encoded><![CDATA[<p>A question that came up in regards to <a href="http://aahacreative.com/2010/07/19/jquery-fastest-method-find-descendents/">jQuery Performance</a> was: is it faster to define the type of element before you set the ID. Basically, is <span class="inline-code">jQuery(&#039;textarea#content_box&#039;)</span> better than <span class="inline-code">jQuery(&#039;#content_box&#039;)</span>? The thinking is that because the latter is more specific, the parser has to do less work to find the element. But this is wrong and ignores what actually happens in the jQuery code. If you were writing CSS, then yes, it would be faster to define the element type first, but for JavaScript, defining the element type just adds more work for jQuery.</p>

<p><span class="post_thumbnail"><img width="590" height="150" title="jquery" alt="jquery" class="attachment-post-hero wp-post-image" src="http://aahacreative.com/wp-content/uploads/2010/07/jquery.jpg"/></span></p>

<h3>Why shouldn&#8217;t you define the element type?</h3>

<p>So even though popular opinion says yes, the real answer is an emphatic no. You should never supply parent elements or element types when defining the ID of an element. Why? The ID of an element is supposed to be unique. While sometimes bad programing styles or unfriendly widgets can cause problems, the ID should always be unique, so there is only one <span class="inline-code">#content_box</span> and it is always going to be a textarea.</p>

<h3>What&#8217;s the difference?</h3>

<p>If you have read the longer post on <a href="http://aahacreative.com/2010/07/19/jquery-fastest-method-find-descendents/">profiling jQuery</a>, you&#8217;ll notice that the fastest method to find any element is to call the #ID of the element directly. Any other call requires jQuery calling Sizzle and parsing the string.</p>

<p>If you use just the ID of an element, it won&#8217;t need to load sizzle and can grab it immediately using the built in JS function <span class="inline-code">getElementById(&#039;content_box&#039;);</span>. The difference of these two expressions is staggering and as my post on profiling JavaScript demonstrated, the difference is an order of magnitude.</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/x3hXwuqMu9s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2010/07/23/jquery-performance-combining-elements-ids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2010/07/23/jquery-performance-combining-elements-ids/</feedburner:origLink></item>
		<item>
		<title>jQuery: Fastest method to find Descendents</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/Eqyfu73DP2I/</link>
		<comments>http://aahacreative.com/2010/07/19/jquery-fastest-method-find-descendents/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 03:50:47 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=541</guid>
		<description><![CDATA[In jQuery, there are 5 different selectors that can find an element&#8217;s descendants. One of these methods, will grab only first-level children but it is included with the other four to demonstrate the differences. By profiling these 5 selectors, we can get a basic idea of which selector is the fastest, but, as you will [...]]]></description>
			<content:encoded><![CDATA[<p>In jQuery, there are 5 different selectors that can find an element&#8217;s descendants. One of these methods, will grab only first-level children but it is included with the other four to demonstrate the differences. By profiling these 5 selectors, we can get a basic idea of which selector is the fastest, but, as you will see from the graphs, the speed of the different browsers heavily influences the speed of the selector. However, looking into the <a href="http://code.jquery.com/jquery-latest.js">jQuery source itself</a> will demonstrate <em>why</em> certain selectors are faster.</p>

<span class="post_thumbnail "><img width="590" height="150" src="http://aahacreative.com/files/2010/07/jquery.jpg" class="attachment-post-hero wp-post-image" alt="jquery" title="jquery" /></span>

<p>I answered this basic question on <a href="http://stackoverflow.com/">StackOverflow</a> just from my experience without reading the jQuery source or performing benchmarks. I was actually surprised that not only was my order correct, but also that jQuery functioned much as I expected it to under the hood. The images and explanations are directly from my answer on <a href="http://stackoverflow.com/users/365695/aaron-harun">StackOverflow</a>. All of the benchmark tests can be run from <a href=" http://jsfiddle.net/QLV9y/1/">jsFiddle</a>, but wait a bit before you try it out.</p>

<h3>All Descendant Selectors in jQuery</h3>

<p>Our methods:</p>

<ol>
    <li><span class="inline-code">$(&quot;.child&quot;, $(&quot;#parent&quot;));</span> (by using a scope)</li>
    <li><span class="inline-code"> $(&quot;#parent&quot;).find(&quot;.child&quot;);</span> (the find() method)</li>
    <li><span class="inline-code">$(&quot;#parent&quot;).children(&quot;.child&quot;);</span> (For immediate children, I use this a lot)</li>
    <li><span class="inline-code">$(&quot;#parent &gt; .child&quot;);</span> (via CSS selector)</li>
    <li><span class="inline-code"> $(&quot;#parent .child&quot;);</span></li>
</ol>

<h3>Ranking the jQuery Selectors</h3>

<h4>Using a Scope and Find</h4>

<p><strong><span class="inline-code">$(&quot;.child&quot;, $(&quot;#parent&quot;))</span></strong> and <strong><span class="inline-code">$(&quot;#parent&quot;).find(&quot;.child&quot;)</span></strong> are identical with the only difference is that <strong>method 1</strong> needs to parse the scope passed and translate it to a call to <span class="inline-code">$parent.find(&quot;.child&quot;).show();</span>.</p>

<p>Let&#8217;s take a look into the source to see why this is,
<pre class="brush: javascript">jQuery.fn = jQuery.prototype = {
	init: function( selector, context ) {
	// Handle HTML strings
		if ( typeof selector === &quot;string&quot; ) {
			if ( match &amp;&amp; (match[1] || !context) ) {
			[...]
			// HANDLE: $(expr, context)
			// (which is just equivalent to: $(context).find(expr)
			} else {
				return jQuery( context ).find( selector );
			}</pre></p>

<p>The jQuery source tells us two things:</p>

<p>1) <strong>method 1</strong> and <strong>method 2</strong> are exactly equivalent. If anything, method 2 will be slightly faster.</p>

<p>2) Any other method will be slower because of the large block of <span class="inline-code">if&#8230;else</span> statements I removed. Parts of the code will show up later,  but basically, if the passed element isn&#8217;t an ID and there is no context passed, jQuery has to use regex to try and understand it.</p>

<h4>Why you should never use CSS text selectors</h4>

<p><strong><span class="inline-code">$(&quot;#parent &gt; .child&quot;);</span></strong> and <strong><span class="inline-code">$(&quot;#parent .child&quot;);</span></strong> both need to parse the selector and then just call: <span class="inline-code">$(&#039;#parent&#039;).children().filter(&#039;.child&#039;)</span> and <span class="inline-code">$(&#039;#parent&#039;).filter(&#039;.child&#039;)</span> respectively.</p>

<p>I never researched it, so didn&#8217;t know exactly why this was, but I recently saw <a href="http://vimeo.com/12529436">this video</a> by <a href="http://paulirish.com">Paul Irish</a> where he explains <em>why</em> it takes longer to process this. The answer is long and technical, but the lines I clipped from the jQuery source above basically just look to see if an #id is passed and then grabs that element directly.</p>

<p>When you have a longer CSS selector like <span class="inline-code">$(&quot;#parent &gt; .child&quot;)</span> jQuery has to load &#8220;<a href="http://sizzlejs.com/">Sizzle</a>&#8221; which is a large and fairly slow CSS-based selectors library. A separate download of Sizzle is only 4KB, but it starts with this beautiful REGEX:</p>

<p><pre class="brush: javascript">var chunker = 
/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|[&#039;&quot;][^&#039;&quot;]*[&#039;&quot;]|[^\[\]&#039;&quot;]+)+\]|\\.|[^ &gt;+~,(\[\\]+)+|[&gt;+~])(\s*,\s*)?((?:.|\r|\n)*)/g</pre></p>

<p>Yipes! Chunker is a bit more like clunker if you ask me.</p>

<h4> A Quick Note on Using <span class="inline-code">.children()</span> versus <span class="inline-code">.find()</span></h4>

<p>Finally, <strong>method 3</strong> will always be the fastest because it uses <span class="inline-code">.children()</span> to do the least amount of work and uses the most direct method to get first-level children.</p>

<h3>Profiling the  Selectors, Speed test</h3>

<p>The difference in perforamnce in the different browsers is staggering. The calculations are in computations per second, so the higher the number, the faster the selector is.</p>

<p>On <strong>Chrome</strong>, Method 3 is the best then method 1/2 and then 4/5</p>

<p><img src="http://tinyurl.com/2clyrbz" alt="jQuery selector performance in Chrome" /></p>

<p>On <strong>Firefox</strong>, Method 3 is still best then method 1/2 and then 4/5</p>

<p><img src="http://tinyurl.com/2e89tp5" alt="jQuery selector performance in Firefox 3.6" /></p>

<p>On <strong>Opera</strong>, Method 3 is still best then method 4/5 and then 1/2</p>

<p><img src="http://tinyurl.com/2a9r9r8" alt="jQuery selector performance in Opera" /></p>

<p>On <strong>IE 8</strong>, while slower overall than other browsers, it still follows the Method 3, 1,2,4,5 ordering.</p>

<p><img src="http://tinyurl.com/2a8ag59" alt="jQuery selector performance on IE8" /></p>

<p>Overall, <strong>method 3</strong> is the overall best method to use as it is called directly and it doesn&#8217;t need to traverse more than one level of child elements unlike method 1/2 and it doesn&#8217;t need to be parsed like method 4/5</p>

<p>Though, keep in mind that in some of these we are comparing apples to oranges as Method 5 looks at all children instead of first-level ones.</p>

<p>Finally, if you want to read all about profiling and perfomance in jQuery check out <a href="http://paulirish.com/2009/perf/">this slideshow</a> by Paul Irish. It&#8217;s a very useful read if you are just starting to learn how to optimize your jQuery.</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/Eqyfu73DP2I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2010/07/19/jquery-fastest-method-find-descendents/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2010/07/19/jquery-fastest-method-find-descendents/</feedburner:origLink></item>
		<item>
		<title>Escaping regular expressions in PHP</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/p4FQjJrrZ8M/</link>
		<comments>http://aahacreative.com/2010/07/10/escaping-regular-expressions-php/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 21:11:18 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=254</guid>
		<description><![CDATA[Escaping dynamic regex strings automatically in PHP is a lot harder than you would think. You can&#8217;t just use a string like&#34;\$myregex&#34; because PHP will try to escape the $. You can&#8217;t even double slash it like \\$myregex because this doesn&#8217;t work in the regex engine. To get both the PHP and the regular expressions [...]]]></description>
			<content:encoded><![CDATA[<p>Escaping dynamic regex strings automatically in PHP is a lot harder than you would think. You can&#8217;t just use a string like<span class="inline-code">&quot;\$myregex&quot;</span> because PHP will try to escape the <span class="inline-code">$</span>. You can&#8217;t even double slash it like <span class="inline-code">\\$myregex</span> because this doesn&#8217;t work in the regex engine. To get both the PHP and the regular expressions to work correctly together, you have to combine <strong>quadruple</strong> slashes with <em>stripslashes</em>.</p>

<span class="post_thumbnail "><img width="583" height="150" src="http://aahacreative.com/files/2010/07/really-long-regex-to-validate-emails-583x150.png" class="attachment-post-hero wp-post-image" alt="really-long-regex-to-validate-emails" title="really-long-regex-to-validate-emails" /></span>

<h3>The Code</h3>

<p><pre class="brush: php">$escapes = array(&#039;.&#039;,&#039;$&#039;,&#039;^&#039;,&#039;[&#039;,&#039;]&#039;,&#039;?&#039;,&#039;+&#039;,&#039;(&#039;,&#039;)&#039;,&#039;*&#039;,&#039;|&#039;,&#039;\\&#039;);

foreach($escapes as $s){
	$r = &quot;\\\\$s&quot;;
	$myregexbase = str_replace($s, stripslashes($r), $myregexbase);
}</pre></p>

<h3>It&#8217;s usage</h3>

<p>So what sort of time would you need this script you ask? Suppose, for example, you have a dynamic list of words that need to be replaced in a block of text. Now, you don&#8217;t want to replace partial matches (&#8220;cat&#8221; should not be replaced in the word &#8220;catch&#8221;), so you need to merge the list of <span class="inline-code">$words</span> with the compete regex.</p>

<p><pre class="brush: php">$escapes = array(&#039;.&#039;,&#039;$&#039;,&#039;^&#039;,&#039;[&#039;,&#039;]&#039;,&#039;?&#039;,&#039;+&#039;,&#039;(&#039;,&#039;)&#039;,&#039;*&#039;,&#039;|&#039;,&#039;\\&#039;);

foreach($escapes as $s){
	$r = stripslashes(&quot;\\\\$s&quot;);
	$name = str_replace($s, $r, $name);
} 
$needle = &#039;@([^a-zA-Z0-9])(&#039;.$name.&#039;)([^&amp;a-zA-Z0-9])@&#039;;</pre></p>

<h3>Why can&#8217;t you use a single slash?</h3>

<p>The problem is that if you use a single slash it will escape the quote is it wrapped in the problem is with the line <span class="inline-code">$r = &quot;\\\\$s&quot;;</span>. If you try to use single quotes around the string, <span class="inline-code">$s</span> is not parsed correctly. If you use <span class="inline-code">$r = &#039;\\\\&#039;.$s;</span> it works fine, but there is no real benefit.</p>

<p>Why the <span class="inline-code">stripslashes</span>? Why not just use <span class="inline-code">$r = &#039;\\&#039;.$s;</span>? Again, it&#8217;s a problem with a collision between the generated regular expression and PHP. When you replace the character, the <span class="inline-code">\\</span> will be taken as a literal <span class="inline-code">\\</span> rather than an escaped <span class="inline-code">\</span>, so the outputs in the regular expression will interpret the backslash escaped rather than the character that was supposed to be escaped.</p>

<h3> Why not use a different hack?</h3>

<p>Sure you could use <span class="inline-code">$r = trim(&#039;\ &#039;).$s;</span>, but it still just a hack, and it is even less understandable. The one benefit of using <span class="inline-code">\\\\</span> with <span class="inline-code">stripslashes</span> is that it is easy to understand what is happening. Extra backslashes have been added. They are going to be stripped away. While the actual output might not be entirely clear, the solution is understandable in that the extra slashes are being removed. Overall, the problem is kinda crazy, and the solution is even crazier and makes no sense but it works.</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/p4FQjJrrZ8M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2010/07/10/escaping-regular-expressions-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2010/07/10/escaping-regular-expressions-php/</feedburner:origLink></item>
		<item>
		<title>FireFox Leading the Pack: Gradients on Buttons</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/DNpS8FYx2cU/</link>
		<comments>http://aahacreative.com/2010/07/03/firefox-leading-the-pack-gradients-on-buttons/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 20:43:55 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[css sprites]]></category>
		<category><![CDATA[firefox]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=234</guid>
		<description><![CDATA[See those wonderfully beautiful article and comment toggle buttons at the top of every post? (Or in the post image for those reading in a feed reader.) Those are images, but they weren&#8217;t created in Photoshop: they are screenshots of buttons designed with Firefox gradients. That&#8217;s right, when I was working on them, I wanted [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://aahacreative.com/wp-content/uploads/2010/06/button.toggle.png" alt="" title="button.toggle" class="alignleft size-full wp-image-238 post_image" /> See those wonderfully beautiful article and comment toggle buttons at the top of every post? (Or in the post image for those reading in a feed reader.) Those are images, but they weren&#8217;t created in Photoshop: they are screenshots of buttons designed with Firefox gradients. That&#8217;s right, when I was working on them, I wanted buttons done entirely in CSS, but none of the other browsers support both gradients on the button and the gradient borders needed to give the button a feeling of depth without images.</p>

<p>However, the code that was used to create these in firefox is as such.</p>

<p><pre class="brush: css">background: -moz-linear-gradient(bottom, #999, #ccc);
	-moz-border-radius:8px;
	-moz-border-bottom-colors: #ccc #999 #6666;
	-moz-border-top-colors:  #ccc #999 #666;
	-moz-border-left-colors: #ccc #999 #666;
	-moz-border-right-colors: #ccc #999 #666;</pre></p>

<p>The first line creates the gradient inside the button, the second creates the curved borders and the final 4 lines create a border gradient 3px wide. The CSS above won&#8217;t work exactly as expected because you need to separately define a border that has the same width as the number of colors, so the entire .toggle class looks like:</p>

<p><pre class="brush: css">.toggle{
	color:#333;
	font-size:1.1em;
	height:31px;
	width:86px;
	border:3px solid #ccc;
	/*//mozilla*/
	background: -moz-linear-gradient(bottom, #999, #ccc);
	-moz-border-radius:8px 8px 8px 8px;
	-moz-border-bottom-colors: #ccc #999 #6666;
	-moz-border-top-colors:  #ccc #999 #666;
	-moz-border-left-colors: #ccc #999 #666;
	-moz-border-right-colors: #ccc #999 #666;
}</pre></p>

<p>The .toggled class is:</p>

<p><pre class="brush: css">.toggled{
	border:2px solid #333;
	background: -moz-linear-gradient(bottom, #AAA,#999);
	-moz-border-bottom-colors: #333 #999;
	-moz-border-top-colors:  #333 #999 ;
	-moz-border-left-colors: #333 #999 ;
	-moz-border-right-colors: #333 #999;
}</pre></p>

<p>And since a hover class increases usability:</p>

<p><pre class="brush: css">.toggle:hover{
	background: -moz-linear-gradient(bottom, #AAA, #EEE);
	-moz-border-bottom-colors: #aaa #ccc #999;
	-moz-border-top-colors:  #aaa #ccc #999 ;
	-moz-border-left-colors: #aaa #ccc #999 ;
	-moz-border-right-colors: #aaa #ccc #999 ;
}</pre></p>

<p>This is not a Google Chrome or IE bashing post, instead of leaving it entirely in CSS and having it degrade in all other browsers to some extent, I took a screenshot and replaced all the CSS with a CSS sprite.</p>

<p><pre class="brush: css">.toggle{
	
	color:#333;
	font-size:1.1em;
	background: url(&#039;./style/images/button.toggle.png&#039;) no-repeat 0 0;
	height:31px;
	width:86px;
	border:0;
}

.toggled{
	background-position:0 -31px;
}</pre></p>

<p>It was actually easier to design the button in Firefox than in something like photoshop especially since I used the <a href="https://addons.mozilla.org/en-US/firefox/addon/60/">WebDeveloper toolbar</a> to write the CSS in the browser itself.</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/DNpS8FYx2cU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2010/07/03/firefox-leading-the-pack-gradients-on-buttons/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2010/07/03/firefox-leading-the-pack-gradients-on-buttons/</feedburner:origLink></item>
		<item>
		<title>Moving WordPress: Replacing the Old URL in the Database</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/kpYJSk6E4ME/</link>
		<comments>http://aahacreative.com/2010/06/10/moving-wordpress-replacing-the-old-url-in-the-database/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 14:53:36 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=203</guid>
		<description><![CDATA[Moving a WordPress website can be quite the hassle, but with the following bit of MySQL it can be a lot easier to switch websites from one host to another. The following is a MySQL search and replace of all fields and text content that could conceivably contain links to your old website. It replaces [...]]]></description>
			<content:encoded><![CDATA[<p>Moving a WordPress website can be quite the hassle, but with the following bit of MySQL it can be a lot easier to switch websites from one host to another. The following is a MySQL search and replace of all fields and text content that could conceivably contain links to your old website. It replaces the old domain name in the options table (those pesky home and siteurls), in the post meta table (for compatibility with random plugins), in the post content (for internal links), in the guid field (for cleanliness), the pinged urls so you don&#8217;t ping yourself all over again, and finally the comment content.</p>

<span class="post_thumbnail "><img width="590" height="150" src="http://aahacreative.com/files/2010/06/change-mysql-options.png" class="attachment-post-hero wp-post-image" alt="change-mysql-options" title="change-mysql-options" /></span>

<h3>Disclaimer</h3>

<p>Please back up your database before using this. I claim no credit if you copy in the new or old website URLs wrong and/or create a <em>sitepocolypse</em>. A couple notes, if you have changed your WordPress prefix, you will need to replace all of instances of &#8220;wp_&#8221; in the MySQL search and replace code above.</p>

<h3>How you do it.</h3>

<p><pre class="brush: sql">update wp_options set option_value = replace(option_value,&#039;http://OLD_WEBSITEURL.com&#039;,&#039;http://NEW_WEBSITEURL.com&#039;);
update wp_postmeta set meta_value = replace(meta_value,&#039;http://OLD_WEBSITEURL.com&#039;,&#039;http://NEW_WEBSITEURL.com&#039;);
update wp_posts set post_content = replace(post_content,&#039;http://OLD_WEBSITEURL.com&#039;,&#039;http://NEW_WEBSITEURL.com&#039;);
update wp_posts set guid = replace(guid,&#039;http://OLD_WEBSITEURL.com&#039;,&#039;http://NEW_WEBSITEURL.com&#039;);
update wp_posts set pinged = replace(pinged,&#039;http://OLD_WEBSITEURL.com&#039;,&#039;http://NEW_WEBSITEURL.com&#039;);
update wp_comments set comment_content = replace(comment_content,&#039;http://OLD_WEBSITEURL.com&#039;,&#039;http://NEW_WEBSITEURL.com&#039;);</pre></p>

<h3> More Disclaimers</h3>

<p>Most importantly, <strong>DO NOT add a trailing slash to either the old or the new URL.</strong> Sometimes it will be there (for example, in a link to &#8220;yoursite.com/somepage&#8221;) and others it won&#8217;t such as in the WordPress siteurl and home options.</p>

<p>Finally, <strong>the simple search and replace will replace strings inside serialized arrays</strong>. So if you use a plugin that stores the site&#8217;s url in it&#8217;s own plugin option array (I know cForms does), it will break all the options you have set. Plugins should never store this information, so if you find a plugin that does, you should contact its author and suggest they modify this behavior. However, there are ways around this for people who run test servers.</p>

<h3>Running test servers easily <small>if they are on localhost</small></h3>

<p>Rather than having http://aahacreative.com and http://localhost/aahacreative, wouldn&#8217;t it be easier just to have http://aahacreative.dev? Well, that&#8217;s how we do things around here, and here is how you can to.  (The reason that searching and replacing ahhacreative.com with aahacreative.dev will work is because they have the same number of letters.)</p>

<p>Basically, what you need to do is to add an entry to your hosts file and add a new website in apache. Just <a href="http://www.survivethedeepend.com/zendframeworkbook/en/1.0/creating.a.local.domain.using.apache.virtual.hosts">follow the instructions</a>. I&#8217;ve never tested, but theoretically, you can also edit your hosts file to point to a server with aahacreative.dev.</p>

<p>Someone want to try it and tell me?</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/kpYJSk6E4ME" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2010/06/10/moving-wordpress-replacing-the-old-url-in-the-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2010/06/10/moving-wordpress-replacing-the-old-url-in-the-database/</feedburner:origLink></item>
		<item>
		<title>CSS only Gradients in All Browsers</title>
		<link>http://feedproxy.google.com/~r/AahaCreative/~3/NNY_lwBV43Q/</link>
		<comments>http://aahacreative.com/2010/06/02/creating-css-only-gradients-in-all-browsers/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 01:31:38 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://aahacreative.com/?p=192</guid>
		<description><![CDATA[Currently, it is possible to create CSS gradients in IE 6, IE7, IE8, Webkit-browsers (Chrome 2.0+ and Safari), and Firefox 3.6+. As of this post, Opera doesn&#8217;t support CSS-only gradient backgrounds. There are other methods to create gradient backgrounds with JavaScript or via the canvas element; however, I prefer the CSS only methods. Each of [...]]]></description>
			<content:encoded><![CDATA[<p>Currently, it is possible to create CSS gradients in IE 6, IE7, IE8, Webkit-browsers (Chrome 2.0+ and Safari), and Firefox 3.6+. As of this post, Opera doesn&#8217;t support CSS-only gradient backgrounds. There are other methods to create <a href="http://slayeroffice.com/code/gradient/">gradient backgrounds with JavaScript</a> or via the <a href="http://github.com/westonruter/css-gradients-via-canvas/">canvas element</a>; however, I prefer the CSS only methods.</p>

<span class="post_thumbnail "><img width="590" height="150" src="http://aahacreative.com/files/2010/06/css-only-gradient.png" class="attachment-post-hero wp-post-image" alt="css-only-gradient" title="css-only-gradient" /></span>

<p>Each of the major browsers have their own syntax to implement CSS gradients because it has not been implemented as standard yet.</p>

<p>Webkit (Chrome and Safari)
<span class="inline-code">background: -webkit-gradient(linear, left top, left bottom, from(#EFEFEF), to(#FFF));</span></p>

<p>Firefox 3.6 and above
<span class="inline-code">background: -moz-linear-gradient(top, #efefef, #FFF);</span></p>

<p>Internet Explorer 6 and IE 7
<span class="inline-code"> filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=&#039;#EFEFEF&#039;,EndColorStr=&#039;#FFF&#039;);</span></p>

<p>Internet Explorer 8
<span class="inline-code">-ms-filter: &quot;progid:DXImageTransform.Microsoft.gradient(startColorstr=#EFEFEF, endColorstr=#FFF)&quot;;</span></p>

<p>Whether you have noticed it or not, this website has a CSS gradient that goes from #EFEFEF at the top of the page to #FFFFFF. (This gradient is exceedingly subtle and most people won&#8217;t notice it consciously, but it helps with visibility and eye fatigue on long posts.) The entire code used is:</p>

<p><pre class="brush: css">.gradient{
	/* For any browser that can&#039;t create a gradient  */
	background-color: #EFEFEF;
	/*//mozilla*/
	background: -moz-linear-gradient(top, #efefef, #FFF);
	/* Chrome/Safari 	 */
	background: -webkit-gradient(linear, left top, left bottom, from(#EFEFEF), to(#FFF));
	/*IE ^/7 */	filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=&#039;#EFEFEF&#039;,EndColorStr=&#039;#FFF&#039;);
	/*IE 8 */
	-ms-filter: &quot;progid:DXImageTransform.Microsoft.gradient(startColorstr=#EFEFEF, endColorstr=#FFF)&quot;;
}</pre></p>

<p>Since each prefixed option is only parsed and understood by only one browser, they can be used in a stack without any incompatibilities or errors. This CSS only covers linear gradients; however, radial and diagonal gradients are possible. Use fancier gradients with care and as much as you may want to, never go from Green to Purple no matter how much you may be tempted.</p>

<h3>Beware the Microsoft Filters</h3>

<p>As a final note, in all versions of IE, the background gradient can interfere with the way text is displayed. The images below shows what happens in IE if you try to lay text over a background gradient.</p>

<div id="attachment_392" class="wp-caption aligncenter" style="width: 310px"><a href="http://aahacreative.com/wp-content/uploads/2010/06/Screenshot.png"><img src="http://aahacreative.com/wp-content/uploads/2010/06/Screenshot-300x74.png" alt="" title="Screenshot Without Gradient" width="300" height="74" class="size-medium wp-image-392" /></a><p class="wp-caption-text">This is how the text is supposed to appear.</p></div>

<div id="attachment_391" class="wp-caption aligncenter" style="width: 310px"><a href="http://aahacreative.com/wp-content/uploads/2010/06/Screenshot-1.png"><img src="http://aahacreative.com/wp-content/uploads/2010/06/Screenshot-1-300x69.png" alt="" title="Screenshot with gradient" width="300" height="69" class="size-medium wp-image-391" /></a><p class="wp-caption-text">As soon as the gradient is added, the text becomes blurred. There is no consistancy and it makes the text hard-to-read.</p></div>

<div id="attachment_390" class="wp-caption aligncenter" style="width: 310px"><a href="http://aahacreative.com/wp-content/uploads/2010/06/Screenshot-2.png"><img src="http://aahacreative.com/wp-content/uploads/2010/06/Screenshot-2-300x75.png" alt="" title="Screenshot with gradient and smaller text" width="300" height="75" class="size-medium wp-image-390" /></a><p class="wp-caption-text">With the gradient and a smaller font, the text looks like it was printed without enough ink.</p></div>

<p>For this reason, I&#8217;ve left <a href="http://aahacreative.com">AahaCreative</a> without a background gradient in Internet Explorer. It&#8217;s a shame, but, hopefully, the IE team will get their acts together for IE 9.</p>

<p>P.S. The image that goes with this post is a screenshot of a CSS-only gradient.</p>
<img src="http://feeds.feedburner.com/~r/AahaCreative/~4/NNY_lwBV43Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://aahacreative.com/2010/06/02/creating-css-only-gradients-in-all-browsers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://aahacreative.com/2010/06/02/creating-css-only-gradients-in-all-browsers/</feedburner:origLink></item>
	</channel>
</rss>

