kodal.de

it´s all about music, design and coding

Archive for the ‘ php ’ Category

Smarty – Tips

February 10, 2010 Smarty, coding, php Comments

Find some tricks on how to use Smarty effectively here.

Array handling in Smarty

Arrays are a bit tricky to handle in Smarty. But only at first glance! If you know your way around, it´s pretty easy to cope with them. Note that you can use virtually every native php-function by using modifiers, which allow you to use smarty as a smart wrapper for all php-functions:

array_push

let´s say you want to push the value “new” to an existing array named $offers. You can simply do so by using this term:

{$offers|@array_push:"new"}

Note the @-identifier, which makes smarty process the given array as a whole, rather than iterating through each element of the array.

This approach has a disadvantage, though. After having executed array_push, the returning result will be piped to smarty and therefore we have an unwanted output of the function´s result.

We can simply avoid this by following this approach:

{assign var="void" value=$offers|@array_push:"new"}

By doing so, we are assigning the return value from array_push to a variable (named “void”). As a result, the return-value is being stored in a variable rather than leading to an unwanted output of the latter.

in_array

if you want to check whether a the given value “new” exists within a given $offers, try this:

{"new"|in_array:$offers}

Popularity: 24% [?]

Using PHP for shell-scripting

January 30, 2010 coding, php Comments

This class is a first step to a comprehensive, and yet handy little collection of helper-functions, which can ease your way along while using php for shell-scripts.

Whereas most of you probably use php as a scripting-language for websites, it´s also a very good alternative to other shell-scripting languages like perl or python. In fact, I find it a lot more useful than regular shell-scripts.

This little class can be used both as a static collection of shell-methods, but you can also create instances of it.

So far, it automatically parses any userparams coming from the commandline, and stores them in a publicly available array named self::$arguments.

More features are to be added soon :)

<?
	/* ----------------------------------------------------- */
	/* coding: timor kodal [pulsar-network]					 */
	/* copyright by: (c) 2010 timor kodal				     */
	/* ----------------------------------------------------- */
	/* contact: [http://pulsar.cc | timor [at] kodal.de      */
	/* ----------------------------------------------------- */
	/* to see this and other scripts in action, please visit */
	/* http://pulsar.cc										 */
	/* ----------------------------------------------------- */
	/* you are free to use my scripts 					     */
	/* according to the creative commons licence cc-nc-by    */
	/* ----------------------------------------------------- */
	/* more information: http://creativecommons.org          */
	/* ----------------------------------------------------- */

     class shell {
		public $arguments;
		public $defaults;
		public $aliases;
		function __construct (Array $args = NULL,Array $aliases = NULL,Array $defaults = NULL) {
			$this->aliases = $aliases;
			$this->defaults = $defaults;
			$this->arguments = self::parseArguments($args,$aliases,$defaults);
		}
		public function getArgs(Array $defaults = NULL) {
			$args = $this->arguments;
			if ($defaults) {
				foreach ($defaults as $k=>$v) {
					if (!isset($args[$k]))
						$args[$k]=$v;
					if (!isset($args[$this->aliases[$k]]) && $this->aliases[$k])
						$args[$this->aliases[$k]]=$v;
				}
			}
			return $args;
		}
		public static function parseArguments (Array $args = NULL, Array $aliases = NULL,Array $defaults = NULL) {
			global $argv;
			if (!$aliases)
				$aliases = Array ("u"=>"user");
			if (!$args)
				$args=$argv;

			$arguments = Array();
			$count = 0;

			// -u=1
			// -u test
			// --user=1
			if (is_array($defaults)) {
				foreach ($defaults as $k=>$v) {
					$arguments[$k]=$v;
					if ($aliases[$k])
						$arguments[$aliases[$k]]=$v;
				}
			}

			foreach ($args as $k=>$v) {
				$pattern = "/\-{1,2}([a-z]+)=(.*)/";
				preg_match($pattern,$v,$matches);
				if ($matches[0]) {
					$arguments[$matches[1]]=$matches[2];
					if ($aliases[$matches[1]])
						$arguments[$aliases[$matches[1]]]=$matches[2];
				} else
					$arguments[]=$v;
			}
			return $arguments;
		}
	}

?>

download class.shell.php

Example:

<?
 require_once("class.shell.php");
 $aliases = Array("u"=>"uid","d"=>"default");

 $shell = new shell(NULL,$aliases,Array("u"=>"root"));

 print_r($shell->getArgs(Array("d"=>"yes")));

// php5 example.shell.php -user=teamore

// output:

// Array
// (
// [u] => root
// [uid] => root
// [0] => initprocmail.php
// [user] => teamore
// [d] => yes
// [default] => yes
// )

?>

download example.shell.php

Popularity: 24% [?]