Get in touch:
01524 851 877
07718 896 553

Nice PHP print_r Formatted Table Function

Posted on Jan 16 2012

Whilst looking through php.net I stumbled across the following function that will print out an array in a nicely formatted table. When dealing with lots of nested array something like this makes it much easier to see all of the data contained within an array.

<?php
 function print_nice($elem,$max_level=10,$print_nice_stack=array()){
 if(is_array($elem) || is_object($elem)){
 if(in_array(&$elem,$print_nice_stack,true)){
 echo "<font color=red>RECURSION</font>";
 return;
 }
 $print_nice_stack[]=&$elem;
 if($max_level<1){
 echo "<font color=red>nivel maximo alcanzado</font>";
 return;
 }
 $max_level--;
 echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>";
 if(is_array($elem)){
 echo '<tr><td colspan=2  style="background-color:#333333;"><strong><font  color=white>ARRAY</font></strong></td></tr>';
 }else{
 echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
 echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>';
 }
 $color=0;
 foreach($elem as $k => $v){
 if($max_level%2){
 $rgb=($color++%2)?"#888888":"#BBBBBB";
 }else{
 $rgb=($color++%2)?"#8888BB":"#BBBBFF";
 }
 echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
 echo '<strong>'.$k."</strong></td><td>";
 print_nice($v,$max_level,$print_nice_stack);
 echo "</td></tr>";
 }
 echo "</table>";
 return;
 }
 if($elem === null){
 echo "<font color=green>NULL</font>";
 }elseif($elem === 0){
 echo "0";
 }elseif($elem === true){
 echo "<font color=green>TRUE</font>";
 }elseif($elem === false){
 echo "<font color=green>FALSE</font>";
 }elseif($elem === ""){
 echo "<font color=green>EMPTY STRING</font>";
 }else{
 echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
 }
 }
 ?>

Resizing An Image With PHP

Posted on Apr 08 2010

I came across this PHP class today that is great for resizing images. It is extremely easy to use and quite a small class. The class file can be downloaded here:

SimpleImage.php

A quick example usage to show how simple it is…

include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->save('picture2.jpg');

The last time I had to do some image resizing with PHP was a number of years ago and I seem to remember it being a lot more complicated than this.

Thanks to White Hat Web Design for making this little class and making it available.

Validating Url Strings In PHP

Posted on Sep 25 2008

I was writing a form a little while ago that requests a url from a user. Obviously it would be nice to validate the string the user input in some way. I am not a fan of writing a regex for something like this when I know that there are bound to be many examples of doing this knocking about already so to save time I once again headed over to google to look around.

I came across this link which has a nice little example of a regex to achieve a good validation.

Below is a copy and paste of building the regex string from that site with brief explanations of what each part achieves.

// SCHEME
$urlregex = "^(https?|ftp)\:\/\/";

// USER AND PASS (optional)
$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
// HOSTNAME OR IP
$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*";
// PORT (optional)
$urlregex .= "(\:[0-9]{2,5})?";
// PATH (optional)
$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";// GET Query (optional)
$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?";// ANCHOR (optional)
$urlregex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?\$";

This builds a nice regex string which can be used with the eregi() function to validate a url string like so:

if (eregi($urlregex, $url))
{
echo "URL Valid";
}
else
{
echo "URL Invalid";
}

This regex will validate nearly all valid url’s and is handy to have knocking about.

Thanks to the original author, whoever that may be…..

Get Next Auto Increment Value

Posted on Sep 24 2008

Earlier today I had the need to try and discover the next auto increment value in a MySQL table. I haven’t needed to do this before so I headed over to google to have a look for how to accomplish it.

A lot of information I found said to use a ‘SELECT MAX…’. This would work fine as long as nothing gets deleted from the end of the database. I ended up finding a nice blog entry here that explains exactly how to do it. The following code snippet taken from the site above shows how to get the next value.

$tablename = "tablename";
$next_increment = 0;
$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult = mysql_query($qShowStatus);

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];

echo "next increment number: [$next_increment]";
?>

Quite a useful piece of info…

Xajax

Posted on Apr 03 2008

Xajax ImageI have been doing some form submission for a site I am working on and was thinking it would be nice to use a bit of AJAX to check the forms and display altered content after the form had been submitted, as opposed to simply reloading the page with different parameters.

I went on over to google and started searching to see if I could find any nice libraries that would make the job a little simpler and came across xajax.

This is a really nice PHP toolkit that enables you to add ajax functionality into your web pages extremely easily. All you have to do is include the xajax libraries, create a new xajax object, register a function with the xajax object and include a call to the xajax registered function in your html form, on a button click for example.

Once inside the xajax function you can perform any processing you wish and then return a bunch of html code to be placed inside a container (a div for example) on the calling page.

There are some great little tutorials on the xajax homepage to get you started on your way to lovely ajax enabled web pages.

Convert MySql timestamp field to Unix Time Stamp

Posted on Mar 20 2008

As anyone who has used MySql and PHP in the past to deal with time and date will probably know MySql timestamp fields are not compatible in anyway with PHP’s date() function. Because of this you need to do conversions in order to use the information from the MySql database. The following function will convert a value from a database into a unix timestamp format for easier manipulation within your PHP code. I have found it quite useful to have around so am putting it up here for others to take advantage of.

/*
* Convert MySql timestamp to unix timestamp
*/
function ts2unix($ts)
{
$year = substr($ts,0,4);
$month = substr($ts,4,2);
$day = substr($ts,6,2);
$hour = substr($ts,8,2);
$minute = substr($ts,10,2);
$second = substr($ts,12,2);
$uts = mktime($hour,$minute,$second,$month,$day,$year);
return ($uts);
}

RSS Script

Posted on Mar 18 2008

I decided this morning that I wanted to add an RSS feed to my blog for the hell of it. A quick trip over to google and I stumbled upon a nice little PHP class to do it all for me called FeedCreator.

This class enables many different types of feeds to be created and is extremely simple to use so thanks to Kai Blankenhorn for this I’m sure I will be using it again in the future.