<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vladimir Cvetic &#187; PHP</title>
	<atom:link href="http://www.hazaah.com/cat/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hazaah.com</link>
	<description>buffle the world</description>
	<lastBuildDate>Thu, 12 Nov 2009 14:30:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>toGDImage() broken: Patching ffmpeg-php 0.6.0</title>
		<link>http://www.hazaah.com/programming/togdimage-broken-patching-ffmpeg-php-060/</link>
		<comments>http://www.hazaah.com/programming/togdimage-broken-patching-ffmpeg-php-060/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 17:51:40 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.hazaah.com/?p=152</guid>
		<description><![CDATA[As fallow-up to previous post (link).

After toying with fresh ffmpeg-php 0.6.0 I kept getting error:
Fatal error: failed to convert frame to gd image

Problem lies in broken function _php_avframe_to_gd_image in ffmpeg_frame.c, in the base ffmpeg-php package.
To fix this problem download this patch file and apply it to ffmpeg_frame.c like so:

cd /path/to/ffmpeg-php-0.6.0/
wget http://hazaah.com/download/ffmpeg_frame.c.patch
patch -p0 ffmpeg_frame.c < ffmpeg_frame.c.patch

Reinstall [...]]]></description>
			<content:encoded><![CDATA[<p>As fallow-up to previous post (<a href="http://www.hazaah.com/ubuntu/how-to-ffmpeg-ffmpeg-php-lame-libogg-libvorbis-flvtool2-mplayer-mencoder-amr/">link</a>).</p>
<p>
After toying with fresh ffmpeg-php 0.6.0 I kept getting error:<br />
<em>Fatal error: failed to convert frame to gd image</em>
</p>
<p>Problem lies in broken function _php_avframe_to_gd_image in ffmpeg_frame.c, in the base ffmpeg-php package.<br />
To fix this problem <strong><a href="http://hazaah.com/download/ffmpeg_frame.c.patch">download this patch file</a></strong> and apply it to ffmpeg_frame.c like so:</p>
<pre>
cd /path/to/ffmpeg-php-0.6.0/
wget http://hazaah.com/download/ffmpeg_frame.c.patch
patch -p0 ffmpeg_frame.c < ffmpeg_frame.c.patch
</pre>
<p>Reinstall ffmpeg-php &#038;&#038; restart apache.</p>
<p>Thats all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/togdimage-broken-patching-ffmpeg-php-060/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Ferdinand.PhotoTag &#8211; Facebook like photo tagging</title>
		<link>http://www.hazaah.com/programming/php/ferdinandphototag-facebook-like-photo-tagging/</link>
		<comments>http://www.hazaah.com/programming/php/ferdinandphototag-facebook-like-photo-tagging/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 14:21:57 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.hazaah.com/?p=147</guid>
		<description><![CDATA[I have just released very simple photo tagging solution. It works much like Facebook tagging and requires Prototype and optionally PHP5/MySQL (you can always make backend from scratch in any lang/db you wish&#8230; it requires novice skills).
Tested in Firefox, IE6+, Safari and Chrome.
Here is the link: http://ferdinand.rs/javascript/ferdinandphototag
]]></description>
			<content:encoded><![CDATA[<p>I have just released very simple photo tagging solution. It works much like Facebook tagging and requires Prototype and optionally PHP5/MySQL (you can always make backend from scratch in any lang/db you wish&#8230; it requires novice skills).</p>
<p>Tested in Firefox, IE6+, Safari and Chrome.</p>
<p>Here is the link: <a href="http://ferdinand.rs/javascript/ferdinandphototag">http://ferdinand.rs/javascript/ferdinandphototag</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/php/ferdinandphototag-facebook-like-photo-tagging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple PHP error handling</title>
		<link>http://www.hazaah.com/programming/simple-php-error-handling/</link>
		<comments>http://www.hazaah.com/programming/simple-php-error-handling/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 13:58:40 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.hazaah.com/?p=111</guid>
		<description><![CDATA[Here is a very simple way of custom handling php errors.
To use code below, apart from including it in your functions file, you will need to call function display_debug(); at the end of every page (assuming you wish to display debug on every page).
Nifty feature is that you don&#8217;t have to worry about turning off [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a very simple way of custom handling php errors.<br />
To use code below, apart from including it in your functions file, you will need to call function display_debug(); at the end of every page (assuming you wish to display debug on every page).</p>
<p>Nifty feature is that you don&#8217;t have to worry about turning off debug on production version of your website. Simply populate $ips array with IP addresses you are using and debug will be displayed only to you.</p>
<p>To display mysql errors in this debug, use trigger_error function (example trigger_error($sql.&#8217; &#8216;.mysql_error($link) ,&#8217;DB_ERROR&#8217;);)</p>
<pre>
$ips = array('127.0.0.1','88.99.109.119');
set_error_handler("ErrorHandler");

function ErrorHandler($errno, $errstr, $errfile, $errline)
{
	global $error_handler;

	if (preg_match("/DB_ERROR/iUs", $errno))
	{
		$error_handler[] = "<strong>DB_ERROR*</strong> $errstr\n" . "Fatal error on line $errline in $errfile";
	}

    switch ($errno) {
   	case E_ERROR:
    case E_USER_ERROR:
        $error_handler[] = "<strong>E_USER_ERROR</strong> $errstr on line $errline in $errfile. $errstr\n";
        break;

    case E_WARNING:
	case E_USER_WARNING:
        $error_handler[] = "<strong>E_USER_WARNING</strong> on line $errline in $errfile. $errstr\n";
        break;

    case E_NOTICE:
	case E_USER_NOTICE:
        $error_handler[] = "<strong>NOTICE</strong> on line $errline in $errfile. $errstr\n";
        break;

	case E_PARSE:
        $error_handler[] = "<strong>E_PARSE</strong> on line $errline in $errfile. $errstr\n";
        break;

	case E_STRICT:
        $error_handler[] = "<strong>E_STRICT</strong> on line $errline in $errfile. $errstr\n";
        break;

	case E_DEPRECATED:
        $error_handler[] = "<strong>E_STRICT</strong> on line $errline in $errfile. $errstr\n";
        break;

    default:
        $error_handler[] = "<strong>UNKNOWN</strong> on line $errline in $errfile. $errstr\n";
        break;
    }
    return true;
}

function display_debug()
{
	global $error_handler, $_GET, $_POST, $_SERVER, $_SESSION, $_COOKIE, $_REQUEST, $ips;

	if (!in_array($_SERVER['REMOTE_ADDR'], $ips))
	{
		return;
	}
	?>
<style>
		#debug {
			background:#F9F7ED;
			font-family:Arial, Helvetica, sans-serif;
			font-size:12px;
			padding:10px;
			height:400px;
			width:98%;
			margin:30px auto;
			border:1px solid #333;
			overflow:auto;
		}
	</style>
<div id="debug">
		<?
		foreach($error_handler as $error)
		{
		?>
<p class="single_error">
				<?=$error?>
			

		<?
		}
		?>
<hr size="1" />
		<strong>$_GET</strong>
<pre><?print_r($_GET);?></pre>
<p>		<strong>$_POST</strong></p>
<pre><?print_r($_POST);?></pre>
<p>		<strong>$_REQUEST</strong></p>
<pre><?print_r($_REQUEST);?></pre>
<p>		<strong>$_COOKIE</strong></p>
<pre><?print_r($_REQUEST);?></pre>
<p>		<strong>$_SERVER</strong></p>
<pre><?print_r($_SERVER);?></pre>
<p>		<strong>$_SESSION</strong></p>
<pre><?print_r($_SESSION);?></pre>
</p></div>
<p>	<?<br />
}</p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/simple-php-error-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sort multi dimensional array</title>
		<link>http://www.hazaah.com/programming/sort-multi-dimensional-array/</link>
		<comments>http://www.hazaah.com/programming/sort-multi-dimensional-array/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 09:35:51 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.hazaah.com/?p=109</guid>
		<description><![CDATA[
/*
 * Sort multi dimensional array
 *
 * @author Vladimir Cvetic < vladimir[ at ]ferdinand.rs >
 * @param $a array Array you wish to sort
 * @param $index mixed Value of the key by wich you wish to sort
 * @param $order string Sort direction
 * @param $natsort bool Natural sorting true or false
 * [...]]]></description>
			<content:encoded><![CDATA[<pre>
/*
 * Sort multi dimensional array
 *
 * @author Vladimir Cvetic < vladimir[ at ]ferdinand.rs >
 * @param $a array Array you wish to sort
 * @param $index mixed Value of the key by wich you wish to sort
 * @param $order string Sort direction
 * @param $natsort bool Natural sorting true or false
 * @param $case_sensitive bool Case sensitive true or false
 */
function Sort2dArray ($a, $index, $order='asc', $natsort=false, $case_sensitive=false)
{
	if(is_array($a) &#038;&#038; sizeof($a)>0)
	{
		foreach(array_keys($a) as $key)
			$temp[$key]=$a[$key][$index];
		if(!$natsort)
			($order=='asc')? asort($temp) : arsort($temp);
		else
		{
			($case_sensitive)? natsort($temp) : natcasesort($temp);
			if($order!='asc')
				$temp=array_reverse($temp,true);
		}
		foreach(array_keys($temp) as $key)
			(is_numeric($key))? $sorted[]=$a[$key] : $sorted[$key]=$a[$key];
		return $sorted;
	}
	return $a;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/sort-multi-dimensional-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smarty plugin: module</title>
		<link>http://www.hazaah.com/programming/smarty-plugin-module/</link>
		<comments>http://www.hazaah.com/programming/smarty-plugin-module/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 09:05:14 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.hazaah.com/?p=105</guid>
		<description><![CDATA[Some time ago I wrote this useful smarty plugin wich will enable you to call method of any php class from smarty html template.
It&#8217;s very simple and it goes:

/**
 * @author Vladimir Cvetic < vladimir[at]ferdinand.rs >
 */
function smarty_function_module($params, &#038;$smarty)
{
	/* Check if required params are valid */
	if (!isset($params['class']))
	{
		$smarty->trigger_error('Module name is not defined!');
		return false;
	}
	if (!isset($params['method']))
	{
		$smarty->trigger_error('Method name is [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I wrote this useful smarty plugin wich will enable you to call method of any php class from smarty html template.<br />
It&#8217;s very simple and it goes:</p>
<pre>
/**
 * @author Vladimir Cvetic < vladimir[at]ferdinand.rs >
 */
function smarty_function_module($params, &#038;$smarty)
{
	/* Check if required params are valid */
	if (!isset($params['class']))
	{
		$smarty->trigger_error('Module name is not defined!');
		return false;
	}
	if (!isset($params['method']))
	{
		$smarty->trigger_error('Method name is not defined!', E_USER_ERROR);
		return false;
	}
	elseif (!method_exists($params['class'],$params['method']))
	{
		$smarty->trigger_error('Method is not defined in module! -> '. $params['class'].'::'.$params['method'], E_USER_ERROR);
		return false;
	}

	return call_user_func($params['class'].'::'.$params['method'], $params);
}
</pre>
<p>Example usage:<br />
{module class=&#8221;CLASS_NAME&#8221; method=&#8221;CLASS_METHOD_NAME&#8221; param1=&#8221;string1&#8243; param2=77}</p>
<p>Your class method will receive one parameter (associative array), which will contain all parameters passed to smarty function. I this example array would look like this:</p>
<pre>
Array
(
    [class] => 'CLASS_NAME'
    [method] => 'CLASS_METHOD_NAME'
    [param1] => 'string1'
    [param2] => 77
)
</pre>
<p>To install this plugin simply create file named function.module.php in smarty plugins folder and copy&#038;paste function to that file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/smarty-plugin-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JLoader PHP Class</title>
		<link>http://www.hazaah.com/programming/php/jloader-php-class/</link>
		<comments>http://www.hazaah.com/programming/php/jloader-php-class/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 11:49:07 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.hazaah.com/?p=98</guid>
		<description><![CDATA[So here is one very simple php class. In past it has proven to be very useful in cases where you need to load multiple javascript files or on large project with a lot of modules (especially if those modules are developed by different persons).
It will help you to avoid double js file loads, it [...]]]></description>
			<content:encoded><![CDATA[<p>So here is one very simple php class. In past it has proven to be very useful in cases where you need to load multiple javascript files or on large project with a lot of modules (especially if those modules are developed by different persons).<br />
It will help you to avoid double js file loads, it will also help you keep only one copy of each js file on server.</p>
<p>Enjoy:</p>
<pre>
/**
 * @author Vladimir Cvetic @ Ferdinand Development
 * @email vladimir@ferdinand.rs
 * @phone +381 60 5000240
 *
 * @version 1.0.0
 *
 * Example 1:
 * $jloader = new JLoader();
 * $jloader->load('prototype', '1.6.0.2');
 * $jloader->printjs();
 * Output:
 * <script type="text/javascript" src="/common/jloader/prototype/prototype.js?ver=1.6.0.2"></script>
 *
 * Example 2:
 * $jloader = new JLoader();
 * $jloader->load('prototype', '1.6.0.2');
 * $jloader->load('prototype', false, '/javascript/my_prototype.js');
 * $jloader->printjs();
 * Output:
 * <script type="text/javascript" src="/javascript/my_prototype.js"></script>
 *
 * Example 3:
 * $jloader = new JLoader();
 * $jloader->repository = "/javascript/";
 * $jloader->load('prototype', '1.6.0.2');
 * $jloader->printjs();
 * Output:
 * <script type="text/javascript" src="/javascript/prototype/prototype.js?ver=1.6.0.2"></script>
 *
 * Example 4:
 * $jloader = new JLoader();
 * $jloader->repository = "/javascript/";
 * $jloader->load('prototype', '1.6.0.2');
 * $jloader->load('mootools', '1.2');
 * $jloader->load('custom_script', '1.0', '/scripts/custom_script.js', array('scriptaculous','jquery'));
 * $jloader->printjs();
 * Output:
 * <script type="text/javascript" src="/javascript/prototype/prototype.js?ver=1.6.0.2"></script>
 * <script type="text/javascript" src="/javascript/mootools/mootools.js?ver=1.2"></script>
 * <script type="text/javascript" src="/javascript/scriptaculous/scriptaculous.js"></script>
 * <script type="text/javascript" src="/javascript/jquery/jquery.js"></script>
 * <script type="text/javascript" src="/scripts/custom_script.js?ver=1.0"></script>
 */
class JLoader{

	public $loaded = array();
	public $repository = "/common/jloader";
	public $home_path = "/www/mysite";

	function JLoader()
	{
		return true;
	}

	/**
	 *
	 * @since 1.0.0
	 *
	 * @param string $handler Valid external file handler name.
	 * @param int|bool $ver Optional. Script version number. Default is false.
	 * @param array|bool $depends Array of valid handler dependencies.
	 * @return bool
	 */
	function load($handler, $ver = false, $src = false, $depends=false)
	{
		global $conf;

		if ($src!=false)
		{
			$src = $src;
		}
		elseif ($ver!=false)
		{
			$src = $this->repository."/$handler/$handler.js?ver=$ver";
		}
		else
		{
			$src = $this->repository."/$handler/$handler.js";
		}

		if ($src!=false)
		{
			if (!file_exists($src))
			{
				trigger_error("ExtLoader::load($handler, $ver, $src) sad file ".$src." doesn't exist.");
				return false;
			}
		}
		else
		{
			if (!file_exists($this->home_path.$src))
			{
				trigger_error("ExtLoader::load($handler, $ver, $src) sad file ".$this->home_path.$src." doesn't exist.");
				return false;
			}
		}
		if ($depends!=false)
		{
			foreach($depends as $script)
			{
				$this->load($script);
			}
		}
		$this->loaded[$handler] = '<script type="text/javascript" src="'.$src.'"></script>';

		return true;
	}

	/**
	 * Print loaded Javascript files.
	 *
	 * @since 1.0.0
	 *
	 * @return unknown
	 */
	function printjs()
	{

		foreach($this->loaded as $script)
		{
			echo $script."\n";
		}
	}

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/php/jloader-php-class/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>&#8220;GOTO&#8221; in PHP?</title>
		<link>http://www.hazaah.com/programming/php/goto-in-php/</link>
		<comments>http://www.hazaah.com/programming/php/goto-in-php/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 08:42:04 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[goto]]></category>
		<category><![CDATA[ifsetor]]></category>
		<category><![CDATA[php notes]]></category>

		<guid isPermaLink="false">http://www.hazaah.com/?p=78</guid>
		<description><![CDATA[I have just been reading up meeting notes from PHP Dev team and I found few very interesting conclusions. One of them regards introducing GOTO in PHP (remember GOTO 10?). 
There has been a lot of discussion on GOTO statements during the years most of them citing &#8220;Goto considered harmful&#8221; [uri]. Even with all those [...]]]></description>
			<content:encoded><![CDATA[<p>I have just been reading up meeting notes from PHP Dev team and I found few very interesting conclusions. One of them regards introducing GOTO in PHP (remember GOTO 10?). </p>
<p>There has been a lot of discussion on GOTO statements during the years most of them citing &#8220;Goto considered harmful&#8221; [<a href="http://david.tribble.com/text/goto.html">uri</a>]. Even with all those comments and articles on GOTO I still haven&#8217;t heard argument against it that doesn&#8217;t resolve to &#8220;it looks ugly and breaks the flow&#8221; at the end.</p>
<p>Sure, there will be a lot of beginners that will use it wrongly&#8230; but it is still very useful when breaking out of a nested loop or nested if statements. If you ask me I am all for it.</p>
<p>Now a bit about implementation&#8230; we will probably see reuse of break keyword instead of goto. Reason for this is that a possible php GOTO is not a real GOTO construct. Break keyword would be extended with &#8220;static label&#8221;:<br />
Cite:</p>
<pre>for ($i = 0; $i < 9; $i++)
{
        if (true) {
                break blah;
        }
        echo "not shown";
blah:
        echo "iteration $i\n";
}</pre>
<p>One of limitations that was discussed (to stop misuse) was restricting the construct so that you can only jump out of a construct is possible... (one example of misuse would be jumping to beginning of foreach() loop).</p>
<p>Another interesting thing was that ifsetor will probably not be introduced.</p>
<p>Full php meeting notes (<a href="http://www.php.net/~derick/meeting-notes.html">uri</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/php/goto-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common validation regular expressions</title>
		<link>http://www.hazaah.com/programming/common-validation-regular-expressions/</link>
		<comments>http://www.hazaah.com/programming/common-validation-regular-expressions/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 09:48:15 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.knowbies.com/programming/common-validation-regular-expressions/</guid>
		<description><![CDATA[<em style="color:#555;font-size:110%;">All regular expressions are tested with php function <a href="http://www.php.net/eregi">eregi</a></em>
<strong>Validate E-Mail</strong>, <strong>Validate URL</strong> and <strong>Validate Date/Time</strong>.]]></description>
			<content:encoded><![CDATA[<p><em style="color:#555;font-size:110%;">All regular expressions are tested with php function <a href="http://www.php.net/eregi">eregi</a></em><br />
<strong>Validate E-Mail</strong></p>
<p>Username can contain letters, numbers, and dots</p>
<pre lang="sql">
^([-a-z0-9._]{2,32}@([a-z0-9-]{1,64}[.]){1,5}([a-z0-9]){2,6}$|$)
</pre>
<p><strong>Validate URL</strong></p>
<p>Domain can contain letter, numbers and dashes. HTTP://, HTTPS:// and FILE:// allowed</p>
<pre lang="sql">
^((http://|https://|file://){0,1}([a-z0-9-]{1,32}[.]){1,10}([a-z0-9]){2,3}(:[0-9]{1,5}){0,1}([/\]{1,3}[a-z0-9_-~.]{0,64}){0,16}([?][a-z0-9=%;+]+){0,1}$|$)
</pre>
<p>Example usage of url validation regular expression would go something like this (php):</p>
<pre lang="php">
if (!eregi("^((http://|https://|file://){0,1}"    // type
.'([a-z0-9-]{1,32}[.]){1,10}([a-z0-9]){2,3}'  // domain
.'(:[0-9]{1,5}){0,1}'				       // port
.'([/\]{1,3}[a-z0-9_-~.]{0,64}){0,16}'	       // directory
.'([?][a-z0-9=%;+]+){0,1}$|$)',		         // options
trim($url_to_validate))) echo "url not valid";
else echo "url is valid";
</pre>
<p><strong>Validate Date/Time</strong></p>
<p>Will check date/time in format &#8216;yyyy-m-d h:m:s&#8217;</p>
<pre lang="sql">^([0-9]{4})[-/.]([0-9]{1,2})[-/.]([0-9]{1,2}) ([0-9]{1,2})(:[0-9]{1,2})(:[0-9]{1,2}){0,1}$</pre>
<p>To fully validate date/time input you might want to use something like this:</p>
<pre lang="php">function datetimeValidation($datetime) {
    if (eregi('^([0-9]{4})[-/.]([0-9]{1,2})[-/.]([0-9]{1,2}) ([0-9]{1,2})(:[0-9]{1,2})(:[0-9]{1,2}){0,1}$', trim($datetime), $t))
	if (checkdate($t[2], $t[3], $t[1]))
	    if (($t[4] < 24)&#038;&#038;($t[5] < 60)&#038;&#038;($t[6] < 60))
		return sprintf('%04d-%02d-%02d %02d:%02d:%02d', $t[1], $t[2], $t[3], $t[4], $t[5], $t[6]);
    return false;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/common-validation-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Guide to simple Database Abstraction</title>
		<link>http://www.hazaah.com/programming/guide-to-simple-database-abstraction/</link>
		<comments>http://www.hazaah.com/programming/guide-to-simple-database-abstraction/#comments</comments>
		<pubDate>Mon, 27 Aug 2007 09:31:32 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.knowbies.com/programming/guide-to-simple-database-abstraction/</guid>
		<description><![CDATA[A database abstraction layer is an application programming interface which unifies the communication between a computer application and databases such as MySQL, Oracle or SQLite. Traditionally, all database vendors provide their own interface tailored to their products which leaves it to the application programmer to implement code for all database interfaces he would like to [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>A database abstraction layer is an application programming interface which unifies the communication between a computer application and databases such as MySQL, Oracle or SQLite. Traditionally, all database vendors provide their own interface tailored to their products which leaves it to the application programmer to implement code for all database interfaces he would like to support. Database abstraction layers reduce the amount of work by providing a consistent API to the developer and hide the database specifics behind this interface as much as possible.</p></blockquote>
<p>
I will try to guide you through creating your own database abstraction php class.
</p>
<h3>1. Creating database object</h3>
<p>Define abstraction class.</p>
<pre class="php" name="code">
class Dapc {
}
</pre>
<p>Next thing we will do is create class method which will allow you to load specific data into php object. This method we will name init(). This method will need primary key, table name and array with table fields. We will define them later, don&#8217;t worry about that.</p>
<pre class="php" name="code">
class Dapc {

  function init() {
    global $mysqli; // db connection holder... object.
    $mysqli->stmt_init();
    $key = $this->primary_key; // primary key
    $sql = "SELECT * FROM ".$this->table_name." where ".$this->primary_key." = ".$this->$key;
    $stmt = $mysqli->query($sql) or trigger_error('SQL QUERY ERROR: '.$sql);
    $obj = $stmt->fetch_array();
    foreach($this->fields as $k=>$v) {
      $this->$k = $obj[$k];
    }
    $this->object_loaded = 'Y';
    return true;
  }
}
</pre>
<p>Now what we have done here? We created sql query which will select all fields from given table ($this->table_name) which primary key ($this->primary_key) has value $this->$key and then loaded all those fields in single object.</p>
<h3>Abstraction Interface</h3>
<p>Above we have created bottom layer of our db abstraction. To use this we will need another layer which I call &#8220;interface&#8221;. This layer will keep you away from database. Lets create it.</p>
<p>First say we have example mysql table looking like this:</p>
<pre class="sql" name="code">
CREATE TABLE email
(
   ID INT NOT NULL AUTO_INCREMENT,
   name VARCHAR(35) NOT NULL,
   email VARCHAR(35) NOT NULL,
   PRIMARY KEY (ID)
);
</pre>
<p>Now that we have mysql table we can create little interface class. This class will extend our Dapc class. We will name this class something like {table_name}DAPC and define some variables. First primary key of our table (email) which is ID, next table name and finally table fields array. Table fields should be exactly like our fields in DB table. Form is &#8216;field_name&#8217;=>&#8217;field_type&#8217; finally we will define simple flag which will tell us if object is loaded or not.</p>
<pre class="php" name="code">
class emailDAPC extends Dapc {
  var $primary_key = 'id';
  var $table_name = 'email';

  protected $fields = array(
   'id'=>'int',
   'name'=>'string',
   'email'=>'string'
  );
  var $object_loaded = 'N';
}
</pre>
<p>Next we will create method for emailDAPC class called initData(). It is very simple and will only call init method from our DAPC class.</p>
<pre class="php" name="code">
function initData() {
    $this->init();
 }
</pre>
<p>And constructor&#8230;</p>
<pre class="php" name="code">
 function emailDAPC($id=-1) {
    if ($id!=-1) {
      $key = $this->primary_key;
      $this->$key = $id;
      $this->initData($id);
    }
    return true;
  }
</pre>
<p>Lets see what have we done so far&#8230; If we load class with value 12, email object will load fields and values from table email.</p>
<pre class="php" name="code">
$email = new emailDAPC(12);
print_r($email);
/*Output
email Object
(
    ...
    [object_loaded] => 'Y'
    [id] => 12
    [email] => 'person@example.com'
    [name] => 'Some Person'
    ...
)
*/
</pre>
<h3>Inserting/Updating DB data</h3>
<p>To insert data in table we will need to create another method in bottom layer. This method will be called insert(). We will do similar thing as with init() :</p>
<pre class="php" name="code">
  function insert() {
    global $mysqli;
    unset($stmt);
    $i = 0;
    foreach($this->fields as $k=>$v) {
      if ($obj->$k != $this->$k) {
        if ($i==0) $a = '';
        else $a = ' , ';
        if ($v=='int') $this->sql_prep .= $a.$k.'='.$this->$k;
        else $this->sql_prep .= $a.$k.'="'.$this->$k.'"';
        $i++;
      }
    }
    $sql = "INSERT INTO ".$this->table_name." SET ".$this->sql_prep;

    trigger_error('--> INSERT');
    trigger_error('Executing SQL: '.$sql);
    $mysqli->stmt_init();

    $stmt = $mysqli->prepare($sql) or trigger_error('SQL QUERY ERROR: '.$sql);
    $stmt->execute();
  }
</pre>
<p>We looped fields array (remember that we defined it in interface class) and create query. Very simple.</p>
<p>To be able to update rows we will create update method:</p>
<pre lang="php" name="code">
  function update() {
    global $mysqli;
    $mysqli->stmt_init();
    $key = $this->primary_key;
    $sql = "SELECT * FROM ".$this->table_name." where ".$this->primary_key." = ".$this->$key;
    $stmt = $mysqli->query($sql) or trigger_error('SQL QUERY ERROR: '.$sql);;
    $obj = $stmt->fetch_array();
    $i = 0;
    foreach($this->fields as $k=>$v) {
      if ($obj->$k != $this->$k) {
        if ($i==0) $a = '';
        else $a = ' , ';
        if ($v=='int') $this->sql_prep .= $a.$k.'='.$this->$k;
        else $this->sql_prep .= $a.$k.'="'.$this->$k.'"';
        $i++;
        trigger_error('<b>PREPARE:</b> <font color="red">'.$obj->$k.'!='.$this->$k.'</font> ('.$key.'='.$this->$key.')');
      }
    }
    unset($stmt);
    $mysqli->stmt_init();
    $sql = 'UPDATE '.$this->table_name.' SET '.$this->sql_prep.' WHERE '.$this->primary_key.'='.$this->$key;
    trigger_error('--> UPDATE');
    trigger_error('Executing SQL: '.$sql);
    $stmt = $mysqli->prepare($sql) or trigger_error('SQL QUERY ERROR: '.$sql);
    $stmt->execute();
  }
</pre>
<p>Again same thing, build query from fields array and exec.</p>
<p>Now to simplify&#8230; we will create another method called saveDapc() If you remember we defined that flag $this->object_loaded we will use this flag so we can know what action is needed (insert or update). If object is loaded and saveDapc() method is called we will assume that update is needed and thus call update method and vice versa.</p>
<pre name="code" class="php">
  function saveDapc() {
    if ($this->object_loaded=='Y') {
      $this->update();
    } else {
      $this->insert();
    }
  }
</pre>
<h3>Saving from interface</h3>
<p>With all this now it is very easy to insert/update database from interface. All we need to do is make another method called save() in our email table interface.</p>
<pre name="code" class="php">
  function save() {
    if (!$this->validate()) {
      return false;
    }
    $this->saveDapc();
  }
 function validate() {
  return true;
 }
</pre>
<p>Here is how you will save data:</p>
<p><b>Insert</b></p>
<pre name="code" class="php">
$email = new emailDAPC();
$email->name='Some person';
$email->email='name@example.com';
$email->save();
// That's all! Data inserted in DB
</pre>
<p>Now say we want to change email for some record. We will simply load it with it&#8217;s id:</p>
<pre name="code" class="php">
$email = new emailDAPC(12);
$email->email='newmail@example.com';
$email->save();
// That's all! Email is changed!
</pre>
<p>Data delete can be done the same way as, say&#8230; update. Simply load object and call method delete().</p>
<p>
As you see with db abstraction it is much easier/faster to work. Beside speed this will also allow to migrate to another sql server with only changing bottom level class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/guide-to-simple-database-abstraction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Wordpress Plugin: ContentGen+</title>
		<link>http://www.hazaah.com/programming/php/wordpress-plugin-contentgen/</link>
		<comments>http://www.hazaah.com/programming/php/wordpress-plugin-contentgen/#comments</comments>
		<pubDate>Tue, 24 Apr 2007 12:12:16 +0000</pubDate>
		<dc:creator>Vladimir Cvetic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress Plugins]]></category>

		<guid isPermaLink="false">http://www.knowbies.com/vladimir-cvetic/something-else/wordpress-plugin-contentgen/</guid>
		<description><![CDATA[I have just released another plugin for wordpress.
You can get more info at: http://www.knowbies.com/contentgen/
ContentGen+ is content generator for Wordpress. It comes in form of a plugin. Main characteristics of ContentGen+ are ease of use, simple installation and hight performance. To improve reliability all content is cached thus making it run very fast.
]]></description>
			<content:encoded><![CDATA[<p>I have just released another plugin for wordpress.</p>
<p>You can get more info at: <a href="http://www.knowbies.com/contentgen/">http://www.knowbies.com/contentgen/</a></p>
<p><em>ContentGen+ is content generator for Wordpress. It comes in form of a plugin. Main characteristics of ContentGen+ are ease of use, simple installation and hight performance. To improve reliability all content is cached thus making it run very fast.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hazaah.com/programming/php/wordpress-plugin-contentgen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
