Removing trailing comas with PHP

php_logo_mediumDealing with trailing comas in lists is common problem for developers. I found the best way to handle this situation in PHP is with the following regular expression.

$string = eregi_replace(',$', '', $string);

Before: string = “2, 6, 9,”
After:
string = “2, 6, 9”

Only the trailing coma will be stripped. If no coma is found, the string simply passes through. It’s a simple and effective solution.

Create WordPress Thumbnails Automatically

create wordpress thumbnails automaticallyWordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data. This meta-data can be used by theme authors to do some pretty cool things.

The theme I am currently using allows for a custom field named “thumb”. If you create the “thumb” custom field in a post and pass in the path to an image, for example “wp-content/uploads/2008/12/wordpress_logo.png”, a thumb nail version of the image will appear next the posts title on the home page of my blog. Awesome stuff.

When I discovered this theme and the “thumb” custom field, I already had 225 posts in my blog. I didn’t want to go back and manually create all the custom fields for the existing posts, so I wrote a PHP script to do it for me.

The PHP script cycles through your existing posts and looks for the first image in the post, and creates the thumb field based on it’s location. The script is configurable, your theme may want a different custom field name, like “thumbnail” or “image”.

This code is provided as is. If you aren’t a developer, you should be messing with stuff like this. Be sure to back up your wordpress database before running this.

Instruction:

  • download this code
  • configure the database connection info and custom field name
  • upload this code onto your server
  • point your browser to the file
  • watch the script do it’s magic
  • remove the file from your server
  • donate $2 to my paypal account ([email protected])

Note: This is a one time deal, when you create new posts you will need to add the “thumb” field and image path manually


			

UIPickerView – spinning multiple components

xcode

I found an interesting issues with the iPhone SDK and the UIPickerView

The didSelectRow method gets called after a users selects a new value on a picker wheel. The method knows what wheel (component) was spun and the index of the newly selected value. This works great for a picker with a single wheel. My picker had 5 wheels in it.

If I spun two components of the picker at the same time. I would only get one call to the didSelectRow method. More specifically, if I spin one component and move another component no call was received until the first component stops spinning, and so the second component move is never registered.

Long story short, I didn’t catch this problem when I submitted my app, “Ka-Ching”, to the iTunes store. As a result of not catching this, Apple rejected my app. I could argue that the iPhone SDK should have so programmatic hooks that would only allow one component to be spun at a time, or even a method to return the state of all of components in the picker.

uipickerview

Trying to figure out a solution to the problem I expanded on my idea of recording the state of all the components in the picker when the didSelectRow method was called. Realistically, by the time that didSelectRow gets processed it is possible that all 5 of the components had changed.

Looking through the SDK documentation for the UIPickerView I came across this:

- (NSInteger)selectedRowInComponent:(NSInteger)component

With this method you can determine the selected row index for any of the components in your picker. you don’t have to rely on the values passed into the didSelectRow method.

The wrong way to do it

- (void)pickerView:(UIPickerView *)thePickerView
 didSelectRow:(NSInteger)row
 inComponent:(NSInteger)component
{
	//get the value of the component that was changed
	if(component == 0)
	{
        	currency_value = [currency objectAtIndex:row];
	}
	else if(component == 1)
	{
        	hundereds_value = [hundereds objectAtIndex:row];

	}
	else if(component == 2)
	{
        	tens_value = [tens objectAtIndex:row];
	}

	else if(component == 3)
	{
        	ones_value = [ones objectAtIndex:row];
	}

	else if(component == 4)
	{
        	change_value = [cents objectAtIndex:row];
	}

	//some code here to save the values

}

The right way to do it

- (void)pickerView:(UIPickerView *)thePickerView
 didSelectRow:(NSInteger)row
 inComponent:(NSInteger)component
{

	//get the values
	NSInteger currency_NewRow = [thePickerView selectedRowInComponent:0];
	NSInteger hundereds_NewRow = [thePickerView selectedRowInComponent:1];
	NSInteger tens_NewRow = [thePickerView selectedRowInComponent:2];
	NSInteger ones_NewRow = [thePickerView selectedRowInComponent:3];
	NSInteger cents_NewRow = [thePickerView selectedRowInComponent:4];

	//some code to save the values 

}

phpBB admin password manual reset

phpbb_logo

Today I ran into a situation where I had forgotten the password to my development instance of phpBB 3. I was stuck in a situation were I needed to reset the password. I had full admin access to the database, so changing it there wouldn’t be the problem. The real problem is that phpBB uses its own password hashing, not MD5.

In a work around, I created a new user and used the password “123456” looking at the database , in the users table of the phpBB install. I saw the “user_password” field was “e10adc3949ba59abbe56e057f20f883e”.

I then changed my admin accounts user to the same string, “e10adc3949ba59abbe56e057f20f883e”.

phpbbpasswordreset

I went to the phpBB login screen, fillled out my username, and entered the password “123456”… BINGO! it worked.

So to save you the work. You can follow what I did or just use these hashes to reset your own password:

Hash: e10adc3949ba59abbe56e057f20f883e
Password: 123456

Hash: $H$9Ae3Uk.ECdWW5ya13M4ErWhr4c.761/
Password: password

I hope this helps someone else out there.