Phone Number Formatting Library for CodeIgnitor

18 Dec

Here is a nifty class I wrote in PHP for a side project of mine. I’m using it with CodeIgnitor, so I’m going to provide the steps to integrate it with your CodeIgnitor application quickly.

Naming Rules for custom classes:

  • File names must be capitalized. For example:  Myclass.php
  • Class declarations must be capitalized. For example:  class Myclass
  • Class names and file names must match.

Create a new file /system/application/libraries/Phoneformat.php

In line 3 that I've highlighted, you will notice that the class name is the same as the file name, without the .php extension of course.

Next, in your autoload.php file in the config folder, add the name of the file in the libraries to auto load (if you want to auto load it that is).

Now to use this to format your number, simply call the function format in the library phoneformat with the string you want to format as parameter. For example:

That's all there is to creating a library, and using it in your CodeIgnitor application. Hope this tutorial/code helped you! Lookout for more similar stuff in the future or write a comment if you need some PHP help :)

Leave a Reply

Great Intellects

You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. — Abraham Lincoln