Truncate string with ellipses

We previously discussed how to calculate the length of a string in PHP and I made mention that using that function is how you would go about truncating a string if it’s over a specific length. I also said that down the road I would discuss the topic at hand, truncating a string with ellipses. Here is a pretty simple function to do just...

Read Full Article →

Get date for Easter in PHP

All the good things about Easter aside, it’s one of those holidays that’s a pain to get the date for. This is because the date of Easter is based on the lunar cycle and Spring Equinox (March 21st). The date of Easter on a given year could fall between March 22nd and April 25th. Fortunately PHP has us covered with a pair of functions,...

Read Full Article →

Serializing and unserializing variables

Serialization in PHP is the act of converting a variable into a storable value. When I say storable, I mean being able to store the result in say, a database or a flat file. This is commonly applied to arrays and objects as they are not simply text strings. Under the hood, PHP uses serialize() for session data as it is just an array....

Read Full Article →

Calculate the length of a string

Calculating the length of a string is a useful feature of any language, in fact it’s one of the few pieces of functionality that has a consistently simple syntax across modern languages. You can calculate the length of a string as part of some server side user input validation or do so to determine if you should truncate a string and add ellipses to...

Read Full Article →

How to get a server’s hostname

The hostname of your server or local system is an easy way to determine which environment you are working with, either local or production. Prior to PHP 5.3 you would need to utilize the php_uname() function and with 5.3+ there is a built-in function for getting the hostname: pre-5.3 $hostname = php_uname('n'); 5.3+ $hostname = gethostname(); Once you have the hostname, you can do...

Read Full Article →