Tim Barsness http://www.timbarsness.com/feed en-us http://blogs.law.harvard.edu/tech/rss Sweetcron tbarsness+timbarsness.com@gmail.com Chipotle Mexican Grill http://www.timbarsness.com/items/view/849 @ Chipotle Mexican Grill

]]>
Mon, 09 Apr 2012 22:39:00 +0000 https://foursquare.com/timbarsness/checkin/4f836521e4b08bbbe81d7348
How do I use PHP preg_replace_callback? http://www.timbarsness.com/items/view/848 I’m working on a project that sends transactional emails from templates using variables in the %%varname%% format, but defines variables in another table as a lookup value, or query. So, I needed to match strings of type %%varname%% which screams regular expressions, but also needed varname to look up what to replace it with. So, instead of using multiple functions, I was able to do this with a callback and preg_replace_callback.

<?php $string = "A complicated gentleman allow me to present, Of all %%asdf%% the arts and faculties the terse embodiment, He&#039;s a great arithmetician who can demonstrate with ease %%name%% That two and two are three, or five, or anything you please; An eminent Logician who can make it clear to you That black is white – when looked at from the proper point of view; A marvelous Philologist who&#039;ll undertake to show %%asdf%% That &#039;yes&#039; is but another and a neater form of &#039;no&#039;.";   echo preg_replace_callback(&#039;(%%.%%)&#039;, create_function( &#039;$matches&#039;, &#039;return getValue($matches)&#039; ), $string );   function getValue($var) { return toupper(str_replace(&#039;%%&#039;, &#039;&#039;, $var)); }   / Outputs: A complicated gentleman allow me to present, Of all ASDF the arts and faculties the terse embodiment, He&#039;s a great arithmetician who can demonstrate with ease NAME That two and two are three, or five, or anything you please; An eminent Logician who can make it clear to you That black is white – when looked at from the proper point of view; A marvelous Philologist who&#039;ll undertake to show ASDF That &#039;yes&#039; is but another and a neater form of &#039;no&#039;. */

I use the function getValue to query the database and get the value of the variable. This function is pretty slick; now, PHP just needs to support callbacks that aren’t created as strings and we’ll be all set.

]]>
Wed, 04 Apr 2012 21:37:00 +0000 http://serversideguy.com/2012/04/04/how-do-i-use-php-preg_replace_callback/
Monte Carlo Bar & Cafe http://www.timbarsness.com/items/view/847 @ Monte Carlo Bar & Cafe

]]>
Fri, 23 Mar 2012 21:08:00 +0000 https://foursquare.com/timbarsness/checkin/4f6ce659e4b0a235d6274210
timbarsness: I just unlocked the "Chipotle Groupie" badge on @foursquare! <a href="http://t.co/seh9HWuH" rel="external">http://t.co/seh9HWuH</a> http://www.timbarsness.com/items/view/846 Fri, 16 Mar 2012 17:27:00 +0000 http://twitter.com/timbarsness/statuses/180706912717910016 Chipotle Mexican Grill http://www.timbarsness.com/items/view/845 @ Chipotle Mexican Grill

]]>
Wed, 14 Mar 2012 16:55:00 +0000 https://foursquare.com/timbarsness/checkin/4f60cd9ce4b00cb4e09b782e
Chipotle Mexican Grill http://www.timbarsness.com/items/view/844 @ Chipotle Mexican Grill

]]>
Tue, 13 Mar 2012 17:14:00 +0000 https://foursquare.com/timbarsness/checkin/4f5f807be4b0b2ac43d74b9b
Common Roots Cafe http://www.timbarsness.com/items/view/843 @ Common Roots Cafe

]]>
Tue, 13 Mar 2012 01:01:00 +0000 https://foursquare.com/timbarsness/checkin/4f5e9c80e4b080c365d10686
Chipotle Mexican Grill http://www.timbarsness.com/items/view/842 @ Chipotle Mexican Grill

]]>
Mon, 12 Mar 2012 19:27:00 +0000 https://foursquare.com/timbarsness/checkin/4f5e4e2de4b0ecea28d251d7
How do I grant a user execute permissions to stored procedures in SQL Server? http://www.timbarsness.com/items/view/841 One thing that comes up fairly frequently with SQL Server permissions is allowing execute permissions to all user created programmability on a database. This frequently needs to be done when migrating servers, allowing the user connecting to execute stored procedures. One way to do this is to set explicit permissions for all securables for each user. This is effective with a limited amount of stored procedures. In doing so however, as the number of stored procedures increases, the feasibility of setting explicit permissions decreases. The above also requires that permissions be explicitly granted for newly created stored procedures.
Another way of doing so is to create a role (which we’ll call db_executor) and granting execute permissions to all programmability (or a well-defined subset if you prefer) to said role, then adding desired users to the role. Doing so looks like this:

CREATE ROLE db_executor GRANT EXECUTE TO db_executor EXEC SP_ADDROLEMEMBER 'db_executor', 'username'

The above:

Creates the role db_executor Grants execute permissions to the role Adds the desired user to the role

Because roles exist within a database, this should be done in the context of the database desiring the programmability required.

]]>
Fri, 02 Mar 2012 13:39:00 +0000 http://serversideguy.com/2012/03/02/how-do-i-grant-a-user-execute-permissions-to-stored-procedures-in-sql-server/
How do I use traits in PHP 5.4 http://www.timbarsness.com/items/view/840 The biggest change in PHP 5.4 is the addition of traits to the object-oriented programmability. Traits prevent code duplication by allowing multiple classes to include a collection of methods implemented as a trait. This means multiple classes can include the same method implementations without duplicating code. Traits improve on interfaces because they include not only the function definition, but also the implementation. Traits can also use other traits, meaning that one trait could be a collection of others. So, if you had traits Roll and Bounce, you could have trait Movements that used both Roll and Bounce.

trait HelloWorld { public function sayHello() { echo 'Hello '; }   public function sayWorld() { echo 'World!'; } }   class MyClass { use HelloWorld; public function sayHelloWorld() { $this->sayhello(); $this->sayWorld(); } }   $h = new MyClass(); $h->sayHelloWorld(); // outputs "Hello World!"

In implementing traits, the keywords trait and insteadof were added to PHP. Usage of trait is outlined above. The PHP keyword insteadof is used for conflict resolution. Here’s an example of how to use insteadof:

trait A { public function smallTalk() { echo 'a'; } public function bigTalk() { echo 'A'; } }   trait B { public function smallTalk() { echo 'b'; } public function bigTalk() { echo 'B'; } }   class Talker { use A, B { B::smallTalk insteadof A; A::bigTalk insteadof B; } }   class Aliased_Talker { use A, B { B::smallTalk insteadof A; A::bigTalk insteadof B; B::bigTalk as talk; } }

]]>
Tue, 31 Jan 2012 14:12:00 +0000 http://serversideguy.com/2012/01/31/how-do-i-use-traits-in-php-5-4/
timbarsness: @ChipotleMedia -- lucky! I've only been to @shophousetweets once. Need an excuse to go back to DC. http://www.timbarsness.com/items/view/839 Tue, 24 Jan 2012 16:36:00 +0000 http://twitter.com/timbarsness/statuses/161849922470162432 timbarsness: @LizLabuz <a href="http://t.co/Cd8JCbOf" rel="external">http://t.co/Cd8JCbOf</a> http://www.timbarsness.com/items/view/838 Fri, 20 Jan 2012 13:14:00 +0000 http://twitter.com/timbarsness/statuses/160349490988056576 timbarsness: @mitchellhislop you at #minnedemo? http://www.timbarsness.com/items/view/837 Thu, 19 Jan 2012 01:35:00 +0000 http://twitter.com/timbarsness/statuses/159811166422573056 timbarsness: @TyOlson @smcpros: Name your players... http://www.timbarsness.com/items/view/836 Sat, 31 Dec 2011 04:08:00 +0000 http://twitter.com/timbarsness/statuses/152964425333227522 timbarsness: Hey, @TyOlson, I challenge you to a fuel: <a href="http://t.co/YCWrPYbG" rel="external">http://t.co/YCWrPYbG</a> http://www.timbarsness.com/items/view/835 Sat, 31 Dec 2011 01:28:00 +0000 http://twitter.com/timbarsness/statuses/152923932759044098 timbarsness: "So, what do you think of qdoba? Because I like it better..." @TyOlson http://www.timbarsness.com/items/view/834 Fri, 30 Dec 2011 01:25:00 +0000 http://twitter.com/timbarsness/statuses/152560816036646913 timbarsness: @ReedDaniels Delicious indeed. Love @ChipotleTweets http://www.timbarsness.com/items/view/832 Thu, 29 Dec 2011 21:09:00 +0000 http://twitter.com/timbarsness/statuses/152496409281708032 timbarsness: @mitchellhislop Oooh very nice!! Time well spent. http://www.timbarsness.com/items/view/833 Thu, 29 Dec 2011 20:20:00 +0000 http://twitter.com/timbarsness/statuses/152484258810839040 Project Euler Problem 17 http://www.timbarsness.com/items/view/831 I’ve been having some fun doing the first few problems of Project Euler and figured I’d share my solution to problem 17 here.
The Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage. The Solution The reason I love this solution is I found a PHP library that writes out these numbers for you. It comes from PECL 1.0 and is part of PHP 5.3 via that NumberFormatter class. This class did most of the work for me.

<? $res = "";   $f = new NumberFormatter("en", NumberFormatter::SPELLOUT);   for($i = 1; $i format($i)); if($i > 100 && $i % 100 != 0) { $res .= 'and'; } } echo strlen($res)."\n";

My first submission was incorrect and I quickly figured out it was because the NumberFormatter class was missing the “and” after the hundreds. I simply added that at the end since we’re only looking for a count and came up with the correct result. Installing the NumberFormatter class in Debian squeeze was pretty easy. I simply had to install the PHP5 international compatibility package: sudo apt-get instal php5-intl

]]>
Wed, 28 Dec 2011 04:17:00 +0000 http://serversideguy.com/2011/12/27/project-euler-problem-17/
How do I perform integer division in PHP http://www.timbarsness.com/items/view/830 PHP’s division operator returns a floating point result from the division operator unless both operands are integers and they are evenly divisible (the result is an integer). I was recently working on Project Euler Problem 13 and needed to do integer division, something I haven’t come across in PHP before.
Integer division in PHP is pretty straight forward. Casting the result as an integer will yield an integer. That is:

var_dump(25/7); // float(3.5714285714286) var_dump((int) (25/7)); // int(3) var_dump(round(25/7)); // float(4)

]]>
Wed, 28 Dec 2011 02:34:00 +0000 http://serversideguy.com/2011/12/27/how-do-i-perform-integer-division-in-php/