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 »

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: 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: 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 »

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 »

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