Dr. Regex Sensei

My coworkers flatter me.

See the Pen Regex Sensei by Bryan Fillmer (@bfillmer) on CodePen.

No Comments »
date Jun 3rd 2014
author Mike
category Geek
tags(none)
 

Allow Multiple Users to Edit WordPress Post

I train clients how to use their WordPress sites, and WordPress’ relatively new post-locking feature has made it impossible for clients to look at the same page or post as me to follow along.

So how do you get rid of this popup?

WordPress Post Locked

Just drop this one line of code anywhere into your functions.php and it’ll prevent that popup from appearing:

Also remember to remove that if you’re in a production environment. WordPress added the post lock for a good reason, and it should only be removed temporarily or if you’re the sole admin.

No Comments »
date Mar 4th 2014
author Mike
category Geek
tags,
 

Git Dev

1. Checkout master code from github.

cd into development directory, then run:

git checkout -b development origin/master

This simultaneously pulls down the master, and switches you into a development branch on local server.

2. Develop on local dev server, and track on remote development branch.

Make changes and updates. Repeat the following as much as needed:

git add .
git commit -m "commit message"
git push origin development:development

This will create a secondary parallel branch in github.

3. Merge development with master in github.

git checkout master
git merge --no-ff development
git branch -d development
git push origin master
git push origin :development
No Comments »
date Sep 14th 2013
author Mike
category Geek
tags
 

PHP: array_merge an array with itself

I recently wanted to merge an array with itself, duplicating all of its own elements and appending them to the original array, over and over until it was thousands of times its original size. It took me a little while to find the best way to do this, so I thought I’d share what I found.

Luckily, you can use PHP’s array_merge() function to double the original array’s size. For example:

$fruits = array( 'Apples', 'Oranges', 'Bananas', 'Strawberries' );
$fruits = array_merge( $fruits, $fruits);
echo implode( ', ', $fruits);

This yields: Apples, Oranges, Bananas, Strawberries, Apples, Oranges, Bananas, Strawberries

Awesome, we’ve made our 4 element array into an 8 element array. But what about making it much bigger? I was running speed tests, and wanted an array that had 10,000 elements or more,* so I needed to keep running these array merges. Doing array_merge( $fruits, $fruits, $fruits, ...) wouldn’t work since each additional argument only adds another 4 elements to the array.

The simplest thing to do is recursively duplicate the array, and compound the results. By doing this, the first iteration yields an 8 element array, the second has 16 elements, the third has 32, etc. Example of how to do this:

$fruits = array( 'Apples', 'Oranges', 'Bananas', 'Strawberries' );
for($i=0; $i<11; $i++) {
    $fruits = array_merge( $fruits, $fruits);
}

By running the for loop 11 times, you end up with an array with 8,192 elements, and increasing it to 12 your result has 16384 elements. If you need more elements, use the following equations to figure out how many times you need to loop through your array:

How many elements you'll end up with:

(2^[number of iterations]) * [starting array count]
Example:
(2^11) * 4 = 8192

How many times to loop to achieve a certain length:

ln( [desired final array count] / [starting array count] ) / ln(2)
Example:
ln( 10000/4 ) / ln(2) = ln(2500) / ln(2) = 11.3
Always iterate to the next highest integer, so 11.3 = 12 iterations

By using 12 iterations, you're sure to get at least 10,000. If you need a more exact number, you can always use array_splice() to truncate the result.

 

* For the speed test I was trying to manipulate database objects, and I only had 4 of them in the database. I wanted to see what would happen if I had tens-of-thousands of them in the database, hence the duplication.

No Comments »
date Jan 2nd 2013
author Mike
category Geek
tags, ,
 

PHP App Development – Zend Framework

At this point of my PHP App Development research, I’ve made a decision to start the project using Zend Framework. During my research, I kept reading that frameworks can help your development immensely. They apparently take a lot of the repetition out of your coding, and generally make development more rapid.

So which framework to chose?

There are a lot to chose from, but the same few kept coming up in my research. Zend and CakePHP were probably the two most highly recommended. I’m no expert at the differences between the two, so I won’t even pretend to list them here. But ultimately I decided to go with Zend, mostly for the reasons listed here.

Installing Zend Framework on Linode VPS

UPDATE: After writing this, I realized that apt-get tends to get behind on updating its Zend package. As of writing this, the latest version of Zend is 1.12.0, and running apt-get installed 1.10.3. To fix the error, I removed the /usr/share/php/libzend-framework-php/Zend/ directory and updated it with the latest version of the Library. To be honest, you should probably just follow this tutorial on installing Zend instead of mine. I will leave this here for my own reference though.

I previously wrote about some of the benefits to Amazon’s AWS hosting, but while I’m still learning new technologies like Zend, I figured it would be best to start learning on my current hosting setup and avoid paying for two plans. Btw, my current plan is a Linode 512 with Ubuntu 10.04 and LAMP.

I spent the last day trying to figure out how to install Zend on Linode. Unfortunately, Zend’s official documentation isn’t great for n00bs. It ended up being really simple to install and get a Hello World created, it just took a long time to find a good tutorial. I finally just got it working, so I thought I should document those steps here:

1. Install Zend on server

This couldn’t be easier. SSH onto your server, and run the following command:

sudo apt-get install zend-framework

That downloads and installs all the necessary components of Zend. I still can’t tell if it installs just the full version of Zend Framework, or if it also installs Zend Server. I can’t even tell you the difference between those two things.

Anyway, this installs a handful of things onto your system. The most important to take note of is a library that should have been installed in the following location:

/usr/share/php/libzend-framework-php/Zend/

We’ll be needing that path here in a minute…

2. Create a project

Now we need to decide where our public website is going to reside, and install all of the necessary Zend components in that location. I happened to have a throw-away URL (we’ll call it mydomain.com) that I’m going to use for development, so I created a directory in the following location where my new website will live:

/srv/www/mydomain.com/tup/

The path is pretty arbitrary (“tup” is short for The Untitled Project), but it should at least be within a sub-directory of /srv/www/ (at least that’s where my default server root is).

So now use the cd command to get to your new directory, and then create the project inside of it:

cd /srv/www/mydomain.com/tup/
zf create project

You will be prompted for a location. Make sure to input the path to the tup directory (or wherever your site is going to be housed).

 Now if you do the command ls -la you should see that Zend added a bunch of stuff in there for you.

3. Include library path

This was the part that kept confusing me. Everywhere I looked, I was instructed to “specify the correct library path” but I had no idea what that meant. There are multiple ways of doing it: adding a new include path to your php.ini file, duplicating the whole library, or creating a symlink to it. Wtf do those mean?

Then I found an awesome YouTube video that cleared everything up. What he shows you is that the new project you just created adds an empty directory called “library”:

/srv/www/mydomain.com/tup/library/

Apparently, this new project installs the basics you’ll need to create a new site, but it doesn’t include the main Zend library files. The main installation we did in step 1 installed those core files in a common location. Remember I told you make note of the Zend library path? We’re about to use that.

First, let me explain the methods we’re NOT going to use to include those files. The php.ini method means that you’re telling php to load those files every time php is used. I have about a half-dozen other sites on my server, none of which use Zend Framework, so it would be a waste to load its libraries every time those sites use php. Then there’s the method of copying that whole Zend library directory, and pasting it into the library directory. The Zend library directory has some 3,000 files taking up nearly 30MB. Duplicating it would unnecessarily waste precious hard-disk space, so I ruled that out.

Instead I created a symlink inside of the library directory and pointed it to the Zend directory. First make sure you’ve cd’ed to library directory, then issue the link command:

cd /srv/www/mydomain.com/tup/library/
ln -s /usr/share/php/libzend-framework-php/Zend/ Zend

So now if you issue a ln -la, it should show you a symbolic link from Zend to the libzend path. We’ve just included the Zend core libraries into our website. Yay!

4. Make it live (add a vhost record)

Now we need to tell Apache to deliver our site by creating a virtual host for this site. If you go to the docs directory in your new project, there’s a README.txt file that has an example VHOST file in it. Copy this content, go to /etc/apache2/sites-available/ and create a new file called mydomain.com. In it, paste the VHOST info Zend provided for you. You might have to make small modifications, but here’s what mine ended up looking like:

    DocumentRoot "/srv/www/mydomain.com/tup/public"
    ServerName mydomain.com
    # This should be omitted in the production environment
    SetEnv APPLICATION_ENV development
     
    <Directory "/srv/www/mydomain.com/tup/public">
         Options Indexes MultiViews FollowSymLinks
         AllowOverride All
         Order allow,deny
         Allow from all
    
     

That “public” directory listed on the second line was created when you created your project, and there should be a default “welcome to zend” index.php file in it. Reload apache with the following command, then navigate to mydomain.com and you should see that index file being displayed.

/etc/init.d/apache2 reload

 

That’s it! At least that’s as far as I’ve gotten so far. We’ll see how it goes from here.

No Comments »
date Sep 2nd 2012
author Mike
category Geek
tags, ,