<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Magp.ie &#187; regular expression</title>
	<atom:link href="http://magp.ie/tag/regular-expression/feed/" rel="self" type="application/rss+xml" />
	<link>http://magp.ie</link>
	<description>A nest for the random, shiny, online tidbits I stumble across...</description>
	<lastBuildDate>Tue, 31 Jan 2012 19:01:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='magp.ie' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/061e340c5da13b5a41ae8016bee03aa8?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Magp.ie &#187; regular expression</title>
		<link>http://magp.ie</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://magp.ie/osd.xml" title="Magp.ie" />
	<atom:link rel='hub' href='http://magp.ie/?pushpress=hub'/>
		<item>
		<title>Filter IP addresses with PHP</title>
		<link>http://magp.ie/2011/09/01/filter-ip-addresses-with-php/</link>
		<comments>http://magp.ie/2011/09/01/filter-ip-addresses-with-php/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 19:33:47 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[blacklist]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[IP address]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[whitelist]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=642</guid>
		<description><![CDATA[You may at some stage want to filter an online service based on IP address. In other words, you may want to block or grant access to a request based on their IP address. This can be handled in PHP &#8230; <a href="http://magp.ie/2011/09/01/filter-ip-addresses-with-php/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=642&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You may at some stage want to filter an online service based on IP address. In other words, you may want to block or grant access to a request based on their IP address. This can be handled in PHP by doing the following.</p>
<p>If you have the IP addresses, then it is trivial.</p>
<p><pre class="brush: php;">

//First check IP address is valid
$request_ip = $_SERVER['REMOTE_ADDR'];

if ( !preg_match( &quot;/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/&quot;, $request_ip ) )
    return false;

$blacklist = array(
'111.222.12.11',
'222.111.21.22',
'221.112.11.12'
);

//check that ip is not blacklisted
if ( in_array( $request_ip, $blacklist ) )
    return false;

</pre></p>
<p>If you want to include a range of IP addresses, best to use a regular expression.</p>
<p><pre class="brush: php;">

$blacklist_ip_range = array(
    '/^122\.244\.(\d+)\.(\d+)/', //for IP address in the range 122.244.0.0 - 122.244.255.255
    '/^123\.(\d+)\.(\d+)\.(\d+)/', //for IP address in the range 123.0.0.0 - 123.255.255.255
);

foreach( $blacklist_ip_range as $ip ) {
    if( preg_match( $ip, $request_ip ) )
       	return false;
    }

</pre></p>
<p>If you have a better solution, then please let me know.</p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/blacklist/'>blacklist</a>, <a href='http://magp.ie/tag/filter/'>filter</a>, <a href='http://magp.ie/tag/ip-address/'>IP address</a>, <a href='http://magp.ie/tag/php/'>php</a>, <a href='http://magp.ie/tag/regular-expression/'>regular expression</a>, <a href='http://magp.ie/tag/whitelist/'>whitelist</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/642/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/642/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/642/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/642/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/642/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/642/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/642/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/642/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/642/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/642/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/642/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/642/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/642/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/642/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=642&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2011/09/01/filter-ip-addresses-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript Tricks; URLEncode, Foreach and Remove last character</title>
		<link>http://magp.ie/2010/05/13/javascript-tricks-urlencode-foreach-remove-last-character/</link>
		<comments>http://magp.ie/2010/05/13/javascript-tricks-urlencode-foreach-remove-last-character/#comments</comments>
		<pubDate>Thu, 13 May 2010 18:04:06 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[urlencode]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=184</guid>
		<description><![CDATA[I have come across some neat (if not mind blowing) tricks with JavaScript recently. Firstly, I needed JavaScript code that copied PHP&#8217;s urlencode(). This function returns an encoded string where all non-alphanumeric characters except - _ . are replaced with &#8230; <a href="http://magp.ie/2010/05/13/javascript-tricks-urlencode-foreach-remove-last-character/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=184&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have come across some neat (if not mind blowing) tricks with JavaScript recently. </p>
<p>Firstly, I needed JavaScript code that copied PHP&#8217;s <a href="http://php.net/manual/en/function.urlencode.php" target="_blank">urlencode()</a>.<br />
This function returns an encoded string where all non-alphanumeric characters except <b>- _ .</b> are replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.</p>
<p>JavaScript has 2 functions to emulate urlencode(), but both fall short&#8230;<br />
The <strong>escape</strong> function encodes all non-alphanumeric characters except <b>* @ / + &#8211; _ .</b><br />
The <strong>encodeURIComponent</strong> function encodes all non-alphanumeric characters except <strong>spaces</strong> and <b>! &#8216; ( ) * ~</b></p>
<p>The winner for me though is encodeURIComponent as it encodes all UTF-8 characters while escape only encodes ISO Latin characters.</p>
<p>With this in hand, <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent" target="_blank">mozilla</a> help provide a neat function.<br />
<span id="more-184"></span><br />
<pre class="brush: jscript;">
function url_encode( s ) {
	return encodeURIComponent( s ).replace( /\%20/g, '+' ).replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /\~/g, '%7E' );
}
</pre></p>
<p>This basically uses regular expressions to do a global replace on characters encodeURIComponent misses.</p>
<p>Another tiny trick that I liked is a JavaScript foreach loop. I use this regularly in PHP but its not exactly obvious in JavaScript.<br />
Use the following code&#8230;</p>
<p><pre class="brush: jscript;">
var some_array = new Array;
some_array['a'] = 1;
some_array['b'] = 2;
some_array['c'] = 3;
for ( var key in some_array ) {
  var value = some_array[key];
  console.log( 'Key: ' + key + ', Value: ' + value ); 
}
//outputs...
// Key: a, Value: 1
// Key: b, Value: 2
// Key: c, Value: 3
</pre></p>
<p>This is particularly handy with associative arrays like above.</p>
<p>Final <em>ickle</em> trick is a bit of code to remove the last character from a string. Use the JavaScript <strong>slice</strong> function and its easy peasy&#8230;</p>
<p><pre class="brush: jscript;">
var some_string = '&quot;102&quot;,&quot;123&quot;,&quot;45&quot;,&quot;583&quot;,';
some_string = some_string.slice(0, -1);
</pre></p>
<p>Why is this handy? Well I use loops to construct formatted stings with some delimiter, like the comma in the code above.<br />
However the string usually ends up with the delimiter added on at the end. So I like to remove it at the end rather than checking if I should not append the delimiter with each iteration of the loop.<br />
I think it is much tidier and it is probably quicker but I am open to correction.</p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/javascript/'>javascript</a>, <a href='http://magp.ie/tag/php/'>php</a>, <a href='http://magp.ie/tag/programming/'>Programming</a>, <a href='http://magp.ie/tag/regular-expression/'>regular expression</a>, <a href='http://magp.ie/tag/urlencode/'>urlencode</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/184/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=184&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2010/05/13/javascript-tricks-urlencode-foreach-remove-last-character/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>Regular Expressions</title>
		<link>http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/</link>
		<comments>http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 22:16:08 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[parse assignment value]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[validate email]]></category>
		<category><![CDATA[validate url]]></category>
		<category><![CDATA[xkcd]]></category>

		<guid isPermaLink="false">http://blogalhost.wordpress.com/?p=28</guid>
		<description><![CDATA[First off, regular expressions are great. They are a handy quick way to validate or parse data and you can use them in almost all languages. But of all things, I forget neat regex&#8217;s and in fairness they are a &#8230; <a href="http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=28&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>First off, regular expressions are great.</p>
<p>They are a handy quick way to validate or parse data and you can use them in almost all languages. But of all things, I forget neat regex&#8217;s and in fairness they are a pita to recall as the syntax is plain nutty.</p>
<p>This is where it ends. I am going to reference all the neat regex&#8217;s in this blog as I come across them rather than rely on mother Google.<br />
<span id="more-28"></span><br />
Starting with a couple of simple functions I use to validate email addresses (thanks WordPress core!) and URLs.<br />
<pre class="brush: php;">
function is_url ( $url ) {
	return ( ! preg_match ( '/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url ) ) ? FALSE : TRUE;
}

function is_email( $email ) {
	// Test for the minimum length the email can be
	if ( strlen( $email ) &lt; 3 ) {
		return false;
	}

	// Test for an @ character after the first position
	if ( strpos( $email, '@', 1 ) === false ) {
		return false;
	}

	// Split out the local and domain parts
	list( $local, $domain ) = explode( '@', $email, 2 );

	// LOCAL PART
	// Test for invalid characters
	if ( !preg_match( '/^[a-zA-Z0-9!#$%&amp;\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
		return false;
	}

	// DOMAIN PART
	// Test for sequences of periods
	if ( preg_match( '/\.{2,}/', $domain ) ) {
		return false;
	}

	// Test for leading and trailing periods and whitespace
	if ( trim( $domain, &quot; \t\n\r&#092;&#048;\x0B.&quot; ) !== $domain ) {
		return false;
	}

	// Split the domain into subs
	$subs = explode( '.', $domain );

	// Assume the domain will have at least two subs
	if ( 2 &gt; count( $subs ) ) {
		return false;
	}

	// Loop through each sub
	foreach ( $subs as $sub ) {
		// Test for leading and trailing hyphens and whitespace
		if ( trim( $sub, &quot; \t\n\r&#092;&#048;\x0B-&quot; ) !== $sub ) {
			return false;
		}

		// Test for invalid characters
		if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
			return false;
		}
	}

	// Congratulations your email made it!
	return true;
}
</pre></p>
<p>Lastly and most recently, I wanted to parse a string of code to find an assignment value.<br />
I came across a neat regex to help me parse out the values of these variables. </p>
<p><pre class="brush: php;">
$code = &quot;var string_variable = Superduper;
var digit_variable = 123456;&quot;;

function get_assignment_value( $needle, $haystack, $type = 'string' ) {
	if( $type == 'digit' )
		preg_match( '/.'.$needle.' = (?P&lt;value&gt;\d+)/', $haystack, $matches );
	else
		preg_match( '/.'.$needle.' = (?P&lt;value&gt;\w+)/', $haystack, $matches );
	
	if( empty( $matches[ 'value' ] ) )
		return false;

	if( $type == 'digit' )
		return (int) $matches[ 'value' ];
	
	return $matches[ 'value' ];
}

var_dump( get_assignment_value( 'string_variable', $code ) ); // string(10) &quot;Superduper&quot;
var_dump( get_assignment_value( 'digit_variable', $code, 'digit' ) ); // int(123456) 
var_dump( get_assignment_value( 'digit_variable', $code  ) ); // string(6) &quot;123456&quot; 
</pre></p>
<p>My main source of guidance on this voodoo <a title="Guide to regexs" href="http://www.regular-expressions.info/" target="_blank">here</a> and their <a href="http://www.regular-expressions.info/reference.html">sometimes hard to find but useful reference</a>.</p>
<p>Also, <a href="http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html" target="_blank">here</a> is a good starter on building a regex.</p>
<p><img class="alignnone" title="Wait, forgot to escape a space.  Wheeeeee[taptaptap]eeeeee." src="http://imgs.xkcd.com/comics/regular_expressions.png" alt="I know regular expressions" width="600" height="607" /></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/parse-assignment-value/'>parse assignment value</a>, <a href='http://magp.ie/tag/regex/'>regex</a>, <a href='http://magp.ie/tag/regular-expression/'>regular expression</a>, <a href='http://magp.ie/tag/validate-email/'>validate email</a>, <a href='http://magp.ie/tag/validate-url/'>validate url</a>, <a href='http://magp.ie/tag/xkcd/'>xkcd</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=28&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2010/03/08/regular-expressions-email-url-parse-assignment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>

		<media:content url="http://imgs.xkcd.com/comics/regular_expressions.png" medium="image">
			<media:title type="html">Wait, forgot to escape a space.  Wheeeeee[taptaptap]eeeeee.</media:title>
		</media:content>
	</item>
	</channel>
</rss>
