Archive for March, 2009

WordPress Uploaded Images – How To Change Absolute URL

Posted on March 29th, 2009 in PHP, Web Development | 3 Comments »

When working with WordPress, you may develop a site on a development server with a different domain than the live server where you intend to deploy the site. When you upload images in a post or a page, WordPress assigns an absolute link as the source of the image. Here is a quick SQL query to run after you migrate your site to the live server so that the absolute URL is updated to reflect the changed domain name.

UPDATE wp_posts set post_content=REPLACE(post_content, 'www.devServer.com/', 'www.liveServer.com/');

UPDATED: PHP Snake Case To Camel Case

Posted on March 20th, 2009 in PHP, Web Development | 1 Comment »

For anyone interested in converting snake_case_strings into CamelCaseStrings, here’s a quick php snippet to do it:

/**
* Take a string_like_this and return a StringLikeThis
*
* @param string
* @return string
*/
function _snakeToCamel($val) {
return str_replace(' ', '', ucwords(str_replace('_', ' ', $val)));
}

Or if you prefer the inline thing…

$val = 'snake_case_string';
$val = str_replace(' ', '', ucwords(str_replace('_', ' ', $val)));
echo $val; // Output: SnakeCaseString

Update: Back To CamelCaseString

The popularity of this post has prompted the natural question on how to go from snake_case_string to the camel case version – SnakeCaseString. This gives me the opportunity to make use of a little known feature of php, the create_function function which allows you to make inline, anonymous, functions and use them as parameters. Take a look at this function to see it in use.

function _camelToSnake($val) {
return preg_replace_callback('/[A-Z]/',
create_function('$match', 'return "_" . strtolower($match[0]);'),
$val);
}

ANOTHER UPDATE: Some people want to get a stringLikeThis rather than a StringLikeThis – keeping the first letter lowercase. This is actually a little trickier to accomplish because there is no lcfirst() function like there is a ucfirst() function [NOTE: lcfirst() is coming in PHP 5.3]. So here is an updated function that returns your camelCaseValue starting with a lowercase letter.

function _snakeToCamel($val) {
$val = str_replace(' ', '', ucwords(str_replace('_', ' ', $val)));
$val = strtolower(substr($val,0,1)).substr($val,1);
return $val;
}

Vim: How To Fold Functions

Posted on March 20th, 2009 in Everyday Tips, PHP, Vim, Web Development | No Comments »

Vi Gang Sign

The is really a tip on how to fold any code block including functions. Navigate your cursor somewhere inside of the code block you want to fold, make sure you are in command/normal mode (press escape if you need to) then type zfa}

To save your folds between vim sessions you need to issue the command :mkview otherwise when you close vim your folds will be lost (your folds, not your code). To make life easier on you, you can have your folds automatically saved for you by adding this to your .vimrc file

au BufWinLeave * mkview
au BufWinEnter * silent loadview

Get Adobe Flash playerPlugin by wpburn.com wordpress themes