PHP in_array() only matches value in the first array position

While trying to create a tagging function for a new blog system I’m currently building, I kept running into trouble when using is_array() to find the tags for each post.

Let’s say we have a tag array that was populated from a text file:

$tags = array( 'a', 'b', 'c' );

And now we want to check if a specific tag is in both arrays:

if ( in_array( 'a', $tags ) ) {

  $response = 'Tag found';

} else {

  $response = 'Tag not found';

}

As we expected, $response = ‘Tag found’. But if we search for ‘b’, $response = ‘Tag not found’ even though we can see very clearly that the value ‘b’ exists in the array $tags.

What’s the solution?

The key to the solution lies in the fact that the array $tags was populated from a text file. Text files can contain all sorts of hidden characters that in_array() can pick up when comparing parameters. The fix is simple. Before passing the $tags array to in_array(), trim up each of the array values like so:

$tags = array_map( 'trim', $tags );

This took me a good 3 hours to figure out. I hope you found this article in less time.

WordPress.com Theme Repository

Lately I’ve been searching for some WordPress themes that incorporate the post-formats feature. I soon realized that there are not too many places to find these themes, but one place stood out and the answer will surprise you.

WordPress.com (no, not wordpress.ORG).

Why?

The Automattic team has done a wonderful job of taking freely-available themes and improving upon them.

One big problem did arise. I couldn’t find any way to download the WordPress.com versions of these great themes.

After searching far and wide I finally stumbled upon this great gem, the WordPress.com theme SVN repository. This repository contains every free theme available for the WordPress.com users with all of the great custom functions. How do you get these great themes on your own self-hosted WordPress installation? Take a look at this:

  1. Open the SSH client of your choice (Terminal for Mac, PuTTY for Windows)
  2. SSH into your web server

    ssh username@domain
  3. Navigate to your WordPress theme directory

    cd wp-content/themes/
  4. Type the following into your SSH client

    svn co https://wpcom-themes.svn.automattic.com/

You now have over 150 quality themes at your disposal. Take a look!


Update

Getting errors with your post-formats? Here’s the solution.