<?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>PHP Reference Book Blog &#187; Tips</title>
	<atom:link href="http://www.phpreferencebook.com/category/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpreferencebook.com</link>
	<description>PHP Reference: Beginner to Intermediate PHP5</description>
	<lastBuildDate>Sat, 03 Jul 2010 04:04:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>MySQL Find Fields in Table not Found in First Table</title>
		<link>http://www.phpreferencebook.com/tips/mysql-query-not-in-exclusion-table/</link>
		<comments>http://www.phpreferencebook.com/tips/mysql-query-not-in-exclusion-table/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 17:49:28 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=358</guid>
		<description><![CDATA[It took a ton of googling, and it was really hard to find the answer. I had two tables, and wanted to do a MySQL Query that seems like a NOT IN between two tables. There are two tables with one unique field that is the same between the tables. The goal is to find [...]]]></description>
			<content:encoded><![CDATA[<p>It took a ton of googling, and it was really hard to find the answer. I had two tables, and wanted to do a MySQL Query that seems like a NOT IN between two tables. There are two tables with one unique field that is the same between the tables. The goal is to find the rows in the second table that are <strong>not</strong> found in the first table based upon the similar field.</p>
<p>I could attempt to explain this, but someone has already done a very good job of this, so I&#8217;d rather just provide a link to their content. The post is from the author of &#8220;High Performance MySQL&#8221;, Baron Schwartz, and covers writing an <a href="http://www.xaprb.com/blog/2005/09/23/how-to-write-a-sql-exclusion-join/">SQL Exclusion Join</a>. The key area of interest is the section on LEFT OUTER joins. He offers this example query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> apples<span style="color: #66cc66;">.</span>Variety
<span style="color: #993333; font-weight: bold;">FROM</span> apples
    <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> oranges
        <span style="color: #993333; font-weight: bold;">ON</span> apples<span style="color: #66cc66;">.</span>Price <span style="color: #66cc66;">=</span> oranges<span style="color: #66cc66;">.</span>Price
<span style="color: #993333; font-weight: bold;">WHERE</span> oranges<span style="color: #66cc66;">.</span>Price <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span></pre></div></div>

<p>When in doubt, find someone smarter to answer the question for you. <img src='http://www.phpreferencebook.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Thanks Baron!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/mysql-query-not-in-exclusion-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Tip: getcwd() for Contents of Current Directory</title>
		<link>http://www.phpreferencebook.com/tips/getcwd-scandir-get-display-contents-current-directory/</link>
		<comments>http://www.phpreferencebook.com/tips/getcwd-scandir-get-display-contents-current-directory/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 14:00:52 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[getcwd]]></category>
		<category><![CDATA[scandir]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=352</guid>
		<description><![CDATA[The getcwd() function is short for &#8216;GET Current Working Directory&#8217;. This can easily be combined with the scandir() function which returns an array of all the files and directories inside the specified directory. This tip was excluded from the book as an oversight. &#160; A quick way to get a list of all the contents [...]]]></description>
			<content:encoded><![CDATA[<p>The getcwd() function is short for &#8216;GET Current Working Directory&#8217;. This can easily be combined with the scandir() function which returns an array of all the files and directories inside the specified directory. This tip was excluded from the book as an oversight.<br />
&nbsp;<br />
A quick way to get a list of all the contents of the current directory is to use the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> preprint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt; pre&gt;'</span><span style="color: #339933;">.</span><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt; /pre&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">getcwd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
preprint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// nicely formatted display of the array</span></pre></div></div>

<p>&nbsp;<br />
Of course, you can skip the print/echo portion if you don&#8217;t wish to display the contents and just use the array to perform other checks, but you get the idea.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/getcwd-scandir-get-display-contents-current-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing strtotime -1 month</title>
		<link>http://www.phpreferencebook.com/tips/fixing-strtotime-1-month/</link>
		<comments>http://www.phpreferencebook.com/tips/fixing-strtotime-1-month/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 18:03:26 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[strtotime]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=325</guid>
		<description><![CDATA[There is a bug for strtotime() when you are on the last day of a month that has 31 days in it. The function, strotime(&#8216;-1 month&#8217;) will return the beginning of the current month, or put another way, 30 days prior. Needless to say, this is annoying. However, it doesn&#8217;t come up very often, and [...]]]></description>
			<content:encoded><![CDATA[<p>There is a bug for <strong>strtotime()</strong> when you are on the last day of a month that has 31 days in it. The function, <strong>strotime(&#8216;-1 month&#8217;)</strong> will return the beginning of the current month, or put another way, 30 days prior. Needless to say, this is annoying. However, it doesn&#8217;t come up very often, and there is a way to fix things: by rolling back the clock 3 extra days. I ran into this problem on a different personal project, and wrote the following function to address the issue. Whenever using strtotime() and negative months, switch in strtotimefix() instead:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> strtotimefix<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span><span style="color: #000088;">$timestamp</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</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;">$timestamp</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$timestamp</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: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'m'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'m'</span><span style="color: #339933;">,</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-1 month'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$timestamp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-3 days'</span><span style="color: #339933;">,</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</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: #000088;">$timestamp</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: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$strtotime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$strtotime</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So what&#8217;s happening? Basically, the function will check if the numeric month is the same between the current month and the month through &#8216;-1 month&#8217;. If so, it subtracts 3 days from the current timestamp, then runs through the strtotime function using the new timestamp. If everything is fine, nothing is altered, so you don&#8217;t have to worry that using the strtotimefix() function will break a perfectly normal strtotime(&#8216;-1 month&#8217;) call. Be advised that if you are not doing a call with &#8216;-x month&#8217;, the function will return an incorrect timestamp by 3 days (I hope to update it to be self-aware and only &#8216;fix&#8217; when the bug would be introduced).</p>
<p><strong>Updated on March 30, 2010:</strong><br />
Changed it to -3 days to deal with February/March.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/fixing-strtotime-1-month/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Common Regular Expressions</title>
		<link>http://www.phpreferencebook.com/tips/common-regular-expressions/</link>
		<comments>http://www.phpreferencebook.com/tips/common-regular-expressions/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 05:38:13 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=278</guid>
		<description><![CDATA[I&#8217;ve gotten better and more comfortable with regular expressions as time has passed, and sometimes I spend timing wading through google for some common regular expressions I want to put into use, because I&#8217;m sure someone has already created it. Well, this isn&#8217;t always true (or easy to find), so I decided to collect some [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve gotten better and more comfortable with regular expressions as time has passed, and sometimes I spend timing wading through google for some common regular expressions I want to put into use, because I&#8217;m sure someone has already created it. Well, this isn&#8217;t always true (or easy to find), so I decided to collect some common Regular Expressions that may benefit readers. It&#8217;s a good idea to keep two things handy if you want to play with Regular Expressions yourself:</p>
<ul>
<li>A copy of the PHP book with a tab on pages 149 and 150 for common RegEx syntax</li>
<li>A link to <a href="http://regexpal.com/" target="_blank">RegExPal</a>, a color coding <a href="http://www.phpreferencebook.com/tips/regex-color-coding/">Regular Expression tester</a>.</li>
</ul>
<h1>Common Regular Expressions:</h1>
<h2>Date and Time RegEx</h2>
<p><strong>Time format (no seconds): </strong><br />
HH:MM am/pm</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #208080;">012</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>\s?<span style="color: #009900;">&#40;</span>am<span style="color: #339933;">|</span>AM<span style="color: #339933;">|</span>pm<span style="color: #339933;">|</span>PM<span style="color: #009900;">&#41;</span>$</pre></div></div>

<p><strong>Date in mm/dd/yyyy format, with an option for m/d/yyyy (exclude zero&#8217;s)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #208080;">012</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span> \<span style="color: #339933;">/.-</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#91;</span><span style="color: #208080;">01</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span> \<span style="color: #339933;">/.-</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">19</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span>\d\d$</pre></div></div>

<p><strong>Date in dd/mm/yyyy format, with an option for d/m/yyyy (exclude zero&#8217;s)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#91;</span><span style="color: #208080;">01</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span> \<span style="color: #339933;">/.-</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #208080;">012</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span> \<span style="color: #339933;">/.-</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">19</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span>\d\d$</pre></div></div>

<h2>Demographics RegEx</h2>
<p><strong>Age in years &#8211; max 122</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>$</pre></div></div>

<p><strong>Height in Feet and Inches:</strong><br />
6&#8217;3&#8243;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #0000ff;">')?\s?([1-9]|1[01])$</span></pre></div></div>

<h2>Contact Information RegEx</h2>
<p><strong>U.S. Phone Number &#8211; parenthesis, periods, dashes, and spaces are allowed:</strong><br />
(123) 456.7890</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>\<span style="color: #009900;">&#40;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span>\<span style="color: #009900;">&#41;</span>\s?<span style="color: #339933;">|</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span>\s\<span style="color: #339933;">.</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>?\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span>\s\<span style="color: #339933;">.</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span>$</pre></div></div>

<p><strong>U.S. Zip Code &#8211; 5 or 9 digit with dash</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>\<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span>$</pre></div></div>

<p><strong>Email Address &#8211; (use preg_match) credit goes to <a href="http://fightingforalostcause.net/misc/2006/compare-email-regex.php" target="_blank">fightingforalostcause.net</a></strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span>_a<span style="color: #339933;">-</span>z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #0000ff;">'+*$^&amp;%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&amp;%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?&lt;![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD</span></pre></div></div>

<h2>Currency RegEx</h2>
<p><strong>Currency &#8211; U.S. Dollars and Cents with commas for multiple&#8217;s of 1000 and a period for the decimal:</strong><br />
$12,000.23</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^\$?<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #009900;">&#41;</span>$</pre></div></div>

<p><strong>Currency &#8211; British Pounds with commas for multiple&#8217;s of 1000 and a period for the decimal:</strong><br />
£12,000.23</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^\u00A3?<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #009900;">&#41;</span>$</pre></div></div>

<p><strong>Currency &#8211; Euros with periods for multiple&#8217;s of 1000 and a comma for the decimal:</strong><br />
€12.000,23</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^\u20AC?<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #009900;">&#41;</span>$</pre></div></div>

<p><strong>Currency &#8211; Euros, French style, with spaces for multiple&#8217;s of 1000 and a comma for the decimal:</strong><br />
€12 000.23</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">^\u20AC?<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>\s<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?<span style="color: #009900;">&#41;</span>$</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/common-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Regular Expressions with Color Highlighting</title>
		<link>http://www.phpreferencebook.com/tips/regex-color-coding/</link>
		<comments>http://www.phpreferencebook.com/tips/regex-color-coding/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 08:01:20 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[ereg]]></category>
		<category><![CDATA[eregi]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[preg_replace]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=236</guid>
		<description><![CDATA[Discovered a great website today while working on a complex Regular Expression: RegExPal.com. The site provides color code highlighting for RegEx syntax, including real-time evaluation of test data. If you, for instance, forget to include a closing or opening parenthesis for a group inside the regular expression, the orphaned parenthesis will be highlighted in red [...]]]></description>
			<content:encoded><![CDATA[<p>Discovered a great website today while working on a complex Regular Expression: <a href="http://regexpal.com/">RegExPal.com</a>. The site provides color code highlighting for RegEx syntax, including real-time evaluation of test data. If you, for instance, forget to include a closing or opening parenthesis for a group inside the regular expression, the orphaned parenthesis will be highlighted in red for easy identification of the error. </p>
<p>Here is a quick screenshot of some of the highlighting in action:<br />
<img src="http://www.phpreferencebook.com/wp-content/uploads/2009/04/regexpal.png" alt="RegExPal Screenshot" title="regexpal" width="567" height="181" class="size-full wp-image-237" /></p>
<p>Be advised that this is based on JavaScript regular expressions, which are comparable to the PERL compatible RegEx functions, such as preg_replace() and preg_match(). It&#8217;s a good idea to get familiar with this syntax, as ereg() and eregi() style functions will be removed from PHP6 when it is released, sometime in the future. Make sure to also checkout the <a href="http://www.amazon.com/gp/product/0596520689?ie=UTF8&#038;tag=phrebobl-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596520689">Regular Expressions Cookbook</a> contributed to by the author of <a href="http://regexpal.com/">RegExPal.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/regex-color-coding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Use isset() Instead of strlen()</title>
		<link>http://www.phpreferencebook.com/tips/use-isset-instead-of-strlen/</link>
		<comments>http://www.phpreferencebook.com/tips/use-isset-instead-of-strlen/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 04:35:04 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[isset]]></category>
		<category><![CDATA[strlen]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=230</guid>
		<description><![CDATA[This tip is courtesy of a great article, 10 Advanced PHP Tips Revisited When referencing an array&#8217;s value via a numeric key, you follow the variable name ($variable) with the numeric index, enclosed by square brackets [5]. e.g. $variable[5] = &#8216;value&#8217; However, when $variable represents a string, using the same syntax will return a string [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>This tip is courtesy of a great article, <a href="http://www.smashingmagazine.com/2009/03/24/10-useful-php-tips-revisited/">10 Advanced PHP Tips Revisited</a></p></blockquote>
<p>When referencing an array&#8217;s value via a numeric key, you follow the variable name ($variable) with the numeric index, enclosed by square brackets [5]. <em>e.g.</em> $variable[5] = &#8216;value&#8217;<br />
However, when $variable represents a string, using the same syntax will return a string with the character at the position specified by the numeric index (0 marks the first character in the $variable string). An example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefg'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output: </strong>string(1) &#8220;c&#8221; </p>
<p>Where this comes into play is when using isset() rather than strlen(). Consider the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefg'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' found!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output: </strong>f found!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefg'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' found!'</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;">echo</span> <span style="color: #0000ff;">'No character found at position 7!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output: </strong>No character found at position 7!</p>
<p>This is faster than using strlen() because, &#8220;&#8230; calling a function is more expensive than using a language construct.&#8221; It&#8217;s little tricks like this that will keep your code lean and efficient. Thanks to <a href="http://shiflett.org/">Chris Shiflett</a> and <a href="http://seancoates.com/">Sean Coates</a> for their contributions to the referenced article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/use-isset-instead-of-strlen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display a Query String Value on a Web Page</title>
		<link>http://www.phpreferencebook.com/tips/display-query-string/</link>
		<comments>http://www.phpreferencebook.com/tips/display-query-string/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 05:15:23 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[$_GET]]></category>
		<category><![CDATA[$_SERVER]]></category>
		<category><![CDATA[query string]]></category>
		<category><![CDATA[urldecode]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=57</guid>
		<description><![CDATA[It seems simple, almost basic, but many people want to display the contents of a query string on a page for the user or just for troubleshooting purposes. Everyone does it, everyone needs to do it, and there are a few different options I&#8217;m going to introduce below. First, our sample URL: http://www.phpreferencebook.com/?variable=value&#038;name=Mario%20Lurig&#038;gender=male%21 or http://www.phpreferencebook.com/?variable=value&#038;name=Mario [...]]]></description>
			<content:encoded><![CDATA[<p>It seems simple, almost basic, but many people want to display the contents of a query string on a page for the user or just for troubleshooting purposes. Everyone does it, everyone needs to do it, and there are a few different options I&#8217;m going to introduce below. First, our sample URL:</p>
<pre>http://www.phpreferencebook.com/?variable=value&#038;name=Mario%20Lurig&#038;gender=male%21</pre>
<p> or</p>
<pre>http://www.phpreferencebook.com/?variable=value&#038;name=Mario Lurig&#038;gender=male!</pre>
<ol>
<li>Display the whole string (everything after the question mark)</li>
<ul>
<li>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'QUERY_STRING'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</li>
<ul>
<li>
<pre>variable=value&#038;name=Mario%20Lurig&#038;gender=male</pre>
</li>
</ul>
</ul>
<li>Display the whole string decoded (convert %## to original characters)</li>
<ul>
<li>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'QUERY_STRING'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</li>
<ul>
<li>
<pre>variable=value&#038;name=Mario Lurig&#038;gender=male!</pre>
</li>
</ul>
</ul>
<li>Show each variable and value as an array (already decoded)</li>
<ul>
<li>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</li>
<ul>
<li>
<pre>Array ( [variable] => value [name] => Mario Lurig [gender] => male!</pre>
</li>
</ul>
<li>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span><span style="color: #339933;">;</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt; /pre&gt;'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</li>
<ul>
<li>
<pre>Array
(
    [variable] => value
    [name] => Mario Lurig
    [gender] => male!
)
</pre>
</li>
</ul>
</ul>
</ol>
<p>There are lots of options from there, so don&#8217;t let this be the end of your exploration of how to display a query string value on a page. If you have any great tricks, feel free to share them in the comments below!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/display-query-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strip_tags() &#8211; Less Than you Bargained For</title>
		<link>http://www.phpreferencebook.com/tips/strip_tags/</link>
		<comments>http://www.phpreferencebook.com/tips/strip_tags/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 22:04:40 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[greater than]]></category>
		<category><![CDATA[htmlspecialchars]]></category>
		<category><![CDATA[less than]]></category>
		<category><![CDATA[strip_tags]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=19</guid>
		<description><![CDATA[While this may be handy for removing HTML from a string, be forewarned that the function is a lot less picky than you may think when it comes to the less than symbol ( &#60; ). First, the section from the PHP book: strip_tags($string [, allowed_tags]) allowed_tags – [optional] $string Remove HTML tags and comments [...]]]></description>
			<content:encoded><![CDATA[<p>While this may be handy for removing HTML from a string, be forewarned that the function is a lot less picky than you may think when it comes to the less than symbol ( &lt; ). First, the section from the <a href="http://www.phpreferencebook.com/purchase/">PHP book</a>:</p>
<hr /><strong>strip_tags($string [, <em>allowed_tags</em>])</strong></p>
<p><em>allowed_tags </em>– [optional] $string</p>
<p>Remove HTML tags and comments from $string. If specific tags should be<br />
excluded, they can be specified inside allowed_tags.</p>
<p>Examples:<br />
<code>$string = "&lt;p&gt;This is a paragraph. &lt;/p&gt;&lt;strong&gt;Yay!&lt;/strong&gt;";<br />
echo strip_tags($string), strip_tags($string, '&lt;p&gt;');</code></p>
<p>HTML Source Code:</p>
<p><code>This is a paragraph. Yay! &lt;p&gt;This is a paragraph. &lt;/p&gt;Yay!</code></p>
<hr />So what happens to the following example, when we want to remove all the tags? Fair warning, something strange happens:</p>
<p><code>$string = "I &lt;strong&gt;love&lt;/strong&gt; this book because it costs &lt;$20.";<br />
echo strip_tags($string);</code></p>
<p>HTML Source Code:</p>
<p><code>I love this book because it costs</code></p>
<p>As you can see, it removed the &lt;$20 portion of the string as well, even without the closing greater than ( &gt; ) tag at the end. Be careful when using <strong>strip_tags()</strong>, especially without specifying the <em>allowed tags</em>, or consider using an alternate such as <strong>htmlspecialchars()</strong> to encode the characters into their html equivalent rather than removing them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/strip_tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display all PHP Errors and Warnings</title>
		<link>http://www.phpreferencebook.com/tips/display-all-php-errors-and-warnings/</link>
		<comments>http://www.phpreferencebook.com/tips/display-all-php-errors-and-warnings/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 05:10:08 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[display errors]]></category>
		<category><![CDATA[display_errors]]></category>
		<category><![CDATA[error reporting]]></category>
		<category><![CDATA[E_ALL]]></category>
		<category><![CDATA[php errors]]></category>
		<category><![CDATA[php warnings]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=18</guid>
		<description><![CDATA[If you wish to see all the PHP errors and warnings in your script, include the following bit of code:
error_reporting(E_ALL);
ini_set('display_errors', '1');]]></description>
			<content:encoded><![CDATA[<p>Every time I was debugging my pages I found myself searching around for this little chunk of code to display PHP errors. So, I put in the book so it was always nearby. Since people still search google endlessly, I thought I would provide it here as well. If you wish to see all the PHP errors and warnings in your script, include the following bit of code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'display_errors'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, continue to beat your head against your keyboard while you continue to hunt down your missing semicolon or closing parenthesis.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/display-all-php-errors-and-warnings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Formatting Characters</title>
		<link>http://www.phpreferencebook.com/tips/formatting-characters/</link>
		<comments>http://www.phpreferencebook.com/tips/formatting-characters/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 13:31:43 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[backspace]]></category>
		<category><![CDATA[carriage return]]></category>
		<category><![CDATA[formatting characters]]></category>
		<category><![CDATA[new line]]></category>
		<category><![CDATA[newline]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[tab]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=17</guid>
		<description><![CDATA[The rules and usage of formatting characters new line, carriage return, tab, and backspace within your PHP code including converting new line to the XHTML line break.]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve all seen them:</p>
<ul>
<li><strong>\n</strong> &#8211; new line</li>
<li><strong>\r</strong> &#8211; carriage return</li>
<li><strong>\t</strong> &#8211; tab</li>
<li><strong>\b</strong> &#8211; backspace</li>
</ul>
<p>But many wonder when to use them or more specifically, why they aren&#8217;t working as expected. So let&#8217;s address the basic usage and rules.</p>
<blockquote><p><em><strong>Rule #1:</strong> When using a formatting character in your code, it must be within &#8220;double quotations&#8221; otherwise it will be taken as a literal backslash and letter.</em></p></blockquote>
<p>When do you use it? When writing to a file with fwrite() or file_put_contents(), sending a text email with mail(), or when adding formatting to pre-populated data in the form element &lt;textarea&gt;.  Now, notice I made no mention of HTML output directly. While it&#8217;s possible to represent new line, tab, carriage return in HTML if it is within the preformatted tags &lt;pre&gt;&lt;/pre&gt;, in most cases these tags are not present and HTML will ignore these formatting characters.</p>
<blockquote><p><em><strong>Rule #2:</strong> Not all computer systems obey the formatting characters the same. When using \n (new line), also include a carriage return (\r) character.</em></p></blockquote>
<p>So what do you do if you have a paragraph, for instance submitted by a &lt;textarea&gt; form, that is preformatted and want it to display in the HTML with the \n (new line) breaks represented? That&#8217;s when you toss the string into the function nl2br(), which changes all \n to the xhtml line break &lt;br /&gt;.</p>
<p><strong>Example:</strong></p>
<p><code>echo nl2br("Hello\n\rWorld\n\r!!!");</code></p>
<p><strong>Results:</strong></p>
<p><code>Hello<br />
World<br />
!!!</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/formatting-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
