Elliott Brueggeman - PHP and Web Development Info, Photography, and More
 
Home | Web Dev Blog & Articles | PHPGraphLib | PHPSimpleChat | SkinnyTip | PHPWeatherLib | Photography | Contact
Posted on October 26, 2008 in PHP by Elliott Brueggeman2 Comments »

I recently came across the need to efficiently parse a string and find what was in between two “marker” strings. I was parsing a logfile with a particular preset format. The line looked like this:

$str = '2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]';

I wanted to be able to easily pull out the ip and page information in a straight-forward way without repeating code. After a little tinkering I came up with a get_token() function that accomplished my goal nicely. The function is below:

function get_token($start_token, $end_token, $haystack, $offset = 0) {
 
	$start = stripos($haystack, $start_token, $offset);
	$end = stripos(substr($haystack, $start), $end_token);
 
	if ($start !== false && $end !== false) {
		$start_pos = $start + strlen($start_token);
		$end_pos = $end - strlen($end_token);
		return substr($haystack, $start_pos, $end_pos);
	}
	return false;
}

Let’s take my above example – if I want to pull out the ip and page information from the given string, I only need to implement the below code:

$str = '2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]';
$ip = get_token('ip:[', ']', $str);
$page = get_token('page:[', ']', $str);

The optional offset argument allows you to specify how many characters into the $haystack that you want to start looking;

Here is another example usage in which we want to pull the title out of some HTML code.

$str = 'This is the body';
$title = get_token('', $str);
Posted on October 2, 2008 in PHP by Elliott Brueggeman2 Comments »

Those programmers who took computer science classes in C++ or Java and then started looking at popular open source PHP scripts may be surprised that the coding standards are somewhat different from what they are used to. Unfortunately, it’s hard to nail down good coding standards for PHP because it has so many syntaxes and does so many things. On one side, PHP is a serious object-oriented language and demanding of java-like syntax, but on another it’s a scripting language and very close to HTML.

One of the older and widely adopted PHP coding standards is based on Todd Hoff’s C++ Coding Standard and is available at http://www.dagbladet.no/development/phpcodingstandard/. Though it is widely used, and (as of the time of writing this) the number one result for “php coding standards” on Google, I give this the least cred because not only was it adopted from a C++ standard but it was not put together by an open source community or company.

The two “most official” coding standards are the PEAR PHP Standards (http://pear.php.net/manual/en/standards.php) and the Zend Framework Coding Standard for PHP (http://framework.zend.com/manual/en/coding-standard.html).

There are also other PHP Standards the are specific to certain projects, like Drupal (http://drupal.org/coding-standards) or Wordpress (http://codex.wordpress.org/WordPress_Coding_Standards), and are certainly valid as these frameworks have thousands of developers adding to their codebase, but they tend to be less detailed the above “official” coding standards.

I personally like the Zend standards the best, as they are the most detailed and easy to follow, but I actually prefer the variable and function naming convention of the Wordpress standard which uses all lowercase words separated by underscores (my_function()) as opposed to the more popular Java style function names (myFunction()) used by the other standards.

At some point in the future, I may write my own coding standards and publish them on this site, combining the best elements from each standard.