PHP Tips & Tutorials

How to link external code source folder in Eclipse PDT projects & Array initializer indentation size?

Code folders may be distributed across different locations and paths but you want to manage them in one Eclipse project. It’s easy to achieve this by linking code source in your Eclipse project. Right click on project Click “Properties” Select “PHP” > “Build Path” Click “Link Source…” Click “Browse” Select the code folder to link …

How to link external code source folder in Eclipse PDT projects & Array initializer indentation size? Read More »

Remove Gmail via Field and Add mailed-by & signed-by with PHP mail()

PHP mail() is a great function to easily send emails from your website server. If you have ever used it before in action, or are currently using mail() to send out emails from your website or application, chances are you would find Gmail to be very persistent in attaching a ‘via’ field to the from …

Remove Gmail via Field and Add mailed-by & signed-by with PHP mail() Read More »

PHP: Round to the Nearest 0.5 (1.0, 1.5, 2.0, 2.5, etc.)

The common practice of rounding, namely the round() function in PHP, is to round to the nearest decimal point, that is, 1.0, 2.0, 3.0, etc. or, 10, 20, 30, etc. For example, echo round(4.25, 0); // 4 echo round(3.1415926, 2); // 3.14 echo round(299792, -3); // 300000 When I’m trying to aggregate all the customer …

PHP: Round to the Nearest 0.5 (1.0, 1.5, 2.0, 2.5, etc.) Read More »

500 Internet Server Error for Incorrect Permissions after Installing suPHP and Uploading PHP Script

Many’s the time after you have uploaded some PHP script to your server and point the web browser to the address it gives 500 Internet Server Error. If you have suPHP installed this is very likely because the uploaded PHP script (files and directories) have wrong permissions set to them. With regards to Linux permissions, …

500 Internet Server Error for Incorrect Permissions after Installing suPHP and Uploading PHP Script Read More »

Convert Object to Array in PHP

There are quite a few ways to do this and each of them has its strengths and weaknesses. Find out if one suits your needs by trying one of the approaches below. Casting with (array) $array = (array) $obj; Problem is this doesn’t convert complex / multi-dimensional objects well. get_object_vars() $array = get_object_vars( $obj ); …

Convert Object to Array in PHP Read More »

PHP preg_match() First Letter of a WordPress Post for Drop Cap Styling

While CSS3 can target the first letter of text inside an element, it’s still not universally supported across major browsers AND it doesn’t work well for elements that have child elements inside. The bullet proof way to target the first letter of a WordPress post would be to capture the content of the post in …

PHP preg_match() First Letter of a WordPress Post for Drop Cap Styling Read More »

PHP Getting the last xx bytes of a large text file

Sometimes the text files are very large and you don’t want to load it in memory before searching through it because it’s inefficient. You just want to get the last few bytes of the file because the latest additions / updates are there. This would help you: $fp = fopen(‘somefile.txt’, ‘r’); fseek($fp, -50, SEEK_END); // …

PHP Getting the last xx bytes of a large text file Read More »

PHP Class for Handling .htpasswd and .htgroup (Member Login & User Management)

Apache is a marvelous web server that offers .htpasswd and .htgroup for controlling restricted access to your website. By help of .htaccess, they work as a member login & user management system that is so simple and easy to deploy. You can even define user groups / roles with it. Basically, .htpasswd defines pairs of …

PHP Class for Handling .htpasswd and .htgroup (Member Login & User Management) Read More »

PHP: Strip or Trim Extra Whitespaces Inside a String

There are raw strings that contain extraly unwanted whitespaces (tabs, spaces, new lines) that you want to get rid of so as to form a normalized string that has only a single spaces between words and sentences. For instance, you may want to transform this string: Morning is here, sunshine is here. Morning is here. …

PHP: Strip or Trim Extra Whitespaces Inside a String Read More »

PHP: Check if A String Contain Only Uppercase / Capital Letters

Sometimes you would want to check if a string is an acronym or an abbreviation by testing if it only contains capitalized letters from A to Z and nothing else. There are 2 ways to accomplish this simple task in PHP. ctype_upper() Use the native ctype_upper() function and you will know if the provided string …

PHP: Check if A String Contain Only Uppercase / Capital Letters Read More »

PHP: Get Mime Type of a File (and Encoding)

While you can somewhat rely on the name extension of a file to determine the mime type, it may not always be accurate. For example, .jpg and .jpeg files are very probably image/jpeg files and .png files are very probably image/png files, but there’s always an exception because file extensions can be faked. To accurately …

PHP: Get Mime Type of a File (and Encoding) Read More »

Redirect 404 Error to Home Page

Other than making your 404 error page user friendly, you can redirect it to your index pages such as the homepage, sitemap, or search page, to make it useful for the users. Instead of relying on them to correct the error themselves, you offer the new orientation. How to redirect a 404 error page to …

Redirect 404 Error to Home Page Read More »

PHP: Store Array in File – Read / Write Arrays in File

You can save re-usable data in database such as MySQL so you can load them among different scripts / script sessions, though it may be a bit overwhelming for simple tasks because you have to set up a whole database and the connection credentials, etc.. File system is probably a better approach with regards to …

PHP: Store Array in File – Read / Write Arrays in File Read More »

PHP: Crontab Class to Add, Edit and Remove Cron Jobs

Provided that your user account on the server has the privileges to access crontab thus can create or remove cron jobs, you can use this PHP class to integrate crontab in your application. I created it for many of my own projects that need crontab to do scheduled jobs. It’s pretty straightforward. Don’t know what …

PHP: Crontab Class to Add, Edit and Remove Cron Jobs Read More »

4 Tips for Zend Studio 8 (Eclipse)

I coded a lot in Zend Studio 5.5.1, the gold old version that’s insanely popular because it made it so easy to code in PHP. As PHP evolves, ZS 5.5.1 gradually falls behind of the edge and it simply becomes imperative that you switch to the latest version of Zend Studio, the 8. The IDE …

4 Tips for Zend Studio 8 (Eclipse) Read More »

PHP: Display Files and Sub-directories of A Directory Recursively as A Tree

Given a directory, how to display the contents (directories and files under it) of it recursively and exhaustively? Here’s the code: $dir = ‘/path/to/directory’; // without trailing slash, can be absolute paths such as ‘/home/jim/public_html’ or relative paths such as ‘samples’ getDirContents($dir); function getDirContents($dir) { if (is_dir($dir)) { $dirs = explode(‘/’, $dir); $last_dir = $dirs[count($dirs) …

PHP: Display Files and Sub-directories of A Directory Recursively as A Tree Read More »

Checklist slides to learn the new features in PHP 5.3 with examples

One of my old Internet friends Brad made a very nice online slide that introduces to you some of the exciting new features of PHP 5.3. I’m most interested in namespace that would make coding in a large project and code reuse much easier, especially for people who find keeping naming conventions a challenge in …

Checklist slides to learn the new features in PHP 5.3 with examples Read More »

PHP: Get Human Readable Time from Seconds

Linux timestamp is a number of seconds from the Epoch of Linux, large number of seconds. When you are counting the time interval between 2 times or 2 dates, the result is usually in seconds as well. Understandably, it’s everything but user friendly to just present the number of seconds to the users, e.g. 153297 …

PHP: Get Human Readable Time from Seconds Read More »

Regular Expression for Date and Time Strings

Often we need the users to enter a valid string of date or time in the form. But how do you validate the strings with regular expressions? In PHP, you can use these functions and regular expressions. RegExp and function to validate against date string: // Default: YYYY-MM-DD function isDate($subject, $separator = ‘-‘) { return …

Regular Expression for Date and Time Strings Read More »

Regular Expressions for Natural Numbers or Positive Integers (1, 2, 3, …), Negative Integers and Non-negative Integers

When I’m developing the online form creator that enables the users to create form fields that accept only certain type of numbers, I need to verify if a given string is a valid natural number such as 1, 2, 3, 4, …. I’m writing the code / functions in PHP but you can literally use …

Regular Expressions for Natural Numbers or Positive Integers (1, 2, 3, …), Negative Integers and Non-negative Integers Read More »

Scroll to Top