Skip navigation

Category Archives: PHP

I’m sold on CodaTextmate is well established, good, etc.  But I’ve found Coda to be the junk for myself.  I just found another reason why I like it as there is a PHP toolkit plugin for Coda.  This plug-in validates PHP w/o running it on the server, cleans up PHP and HTML code, and removes whitespace & comments.  Extremely useful. 


I wrote a function today to format user phone numbers to “xxx xxx-xxx”.  If anyone has a better way of doing this, I’d love to hear it.  For instance, I’d like to know how to insert a character into a certain location inside a string.  As it is, I had to split the string up in order to add whitespace or hypens. 

//function to format US phone numbers to xxx xxx-xxxx
function FormatPhone($Input){
   //trim whitespace off ends
   $Input = trim($Input);

   //trim out whitespace from between numbers
   $Input = str_replace(” “,””,$Input);

   //trim out all characters that are not integers
   $Input = ereg_replace(“[^0-9]”,””,$Input);

   //Count all characters
   $CharCount = strlen($Input);

   //if length is greater than 10, just take last 10 digits
      //trim off all but last 10 digits
     $Input = substr($Input, -10);

   //FORMAT number
   //split area code out
   $AreaCode = substr($Input,0,3);

   //add whitespace to the end of area code
   $AreaCode = str_pad($AreaCode, 4);

   //split out prefix and suffix
   $Prefix = substr($Input,3,3);
   $Suffix = substr($Input,6);

   //add hypen to prefix
   $Prefix = str_pad($Prefix, 4, ‘-‘, STR_PAD_RIGHT);

   //add it all up
   $PhoneNum = $AreaCode.$Prefix.$Suffix;

   //return the new phone number
   return $PhoneNum;

We use PHP and MySQL extensively here at Frontier.  We’ve found it to be the most cost efficient way for us to accomplish things on the web quickly and efficiently.  During the course of our development, we’ve constantly been looking for better ways to do things that are commonly done in the code world.  In that spirit, DevShed has an article on building a PHP basedActive Record Pattern” and “Database abstraction layer” that simplifies and unifies MySQL manipulations via PHP.  Really good stuff. 

Although I’m sure many people already knew the answer to this, I wanted to post the solution mainly so that I don’t forget.  I spent about 30 mins looking for the answer again yesterday since I had forgotten it from the last time I used it. 

To store an array (even a multi-dimensional array) from PHP into MySQL, use the serialize() and unserialize() functions.  There’s other ways of doing it I’m sure, but this has proven quick and painless for myself.  So, sample code might be:

//To insert
$SampleArray = array();
$SampleArray = serialize($SampleArray);

$sql = “INSERT INTO table (‘ArrayField’) VALUES (‘$SampleArray’);
//Run sql statement

//To retrieve
$SampleArray = unserialize($row_table[‘ArrayField’]);

I’ve seen some debate floating around about manipulating MySQL via the command line or via a GUI app like Sequel Pro, Navicat, etc

I personally are using both the command line and a GUI.  I mainly use the Sequel Pro for table structure manipulation and browsing table contents similar to PHPMyAdmin.  However, with Coda’s nice built command line interface, I use the command line to insert, select, update, etc. 

Is there any credence to this argument?  Is there really a GUI that can easily replace the command line?  Should I be creating my statements in a GUI? 

As a self-taught PHP and JS developer (I still consider myself to be somewhat of a n00b), I’ve not had the luxury of being taught the “standard” ways to do things.  While myself and the other people that have helped with our site over time have done pretty well all things considered, I’m starting to look for ways in which myself and anyone else who creates code for us can do so in a standarized way, as much as possible. 

I’ve floated back and forth on things like where to place brackets, variable name conventions, etc.  I’ve found that the PEAR coding standards seem to be what most people are relying on.  Even so, thanks to a post from Big Blue Hat, I can help prevent issues in the future with my PHP scripts.  I had no idea that one could leave off the ending ” ?> ” from a PHP script without causing problems.  Doing so prevents whitespace after the closing tag from causing issues.