Search:
Main Menu
Login | RSS |

Quick PHP Code Tips and Examples

PHP Programming Tips, Tutorials and Source Code Examples for newbie

Spam Protected Contact Form example (using Akismet)

May 9th, 2007 by Jon Moffet

This is an example of an enhanced contact form previously featured here and here . I've improved the contact form example to include Akismet spam protection in order to make it more resistant to spam attack.

Demonstration

plain_akismet.jpg

The example includes :

  • PHPMailer class for sending mail through your own SMTP server
  • Akismet Anti-spam class
  • Table-less contact form

Feel free to download the example to be used in your own application : plain_akismet.zip

Tags: , , , , , , ,

Posted in Uncategorized | 2 Comments »

How to switch to PHP5 in Cpanel based web hosting

May 9th, 2007 by Jon Moffet

Some web hosting provider such as e-ruang offers choices of using PHP4 or or PHP5 in their hosting. To avoid conflict with existing scripts, they will usually offer PHP 4 by default.

But for obvious reasons, you may want to use PHP5 as it supports more functionalities and more robust than PHP4. To enable PHP5 for your account, you need to create a .htaccess file containing :

CODE:
  1. AddHandler application/x-httpd-php5 .php .php4 .php3 .phtml

Save the .htaccess file, and upload it to the root directory of your server.

Then upload a php file containing phpinfo() function to test whether PHP5 has been enabled or not. If the modification has not been successful then you should contact your web hosting provider and ask about the appropriate way to activate PHP5 functionality.

Tags: , , ,

Posted in Uncategorized | No Comments »

Using SQLite in PHP 5

May 9th, 2007 by Jon Moffet

Here is a well-written step-by-step tutorial on how to use SQLite in PHP 5. The tutorial was written by Ilia Alshanetsky and featured in Zend Developer Zone.

SQLite Introduction

For those who don't know, SQLite is a lightweight embedded database management system, it is available as the default database engine for PHP5. Due to its embedded nature, SQLite store its database on a single file and is significantly faster than MySQL. Backing up SQLite database is as simple as copying its database file to a backup media.

SQLite is an excellent choice for creating and deploying portable web application without the need to do complex db setups on target machines.

Tags: , ,

Posted in Uncategorized | 1 Comment »

Truncate long text with PHP (excerpt function)

May 4th, 2007 by Jon Moffet

Truncate is a handy function which can truncate a long text into specific length string and adds elipsis (...) after it.

This is an example of how the function "truncate($text,47)" works :
Before

CODE:
  1. This is a very long text that I type as an example to show you the functionality of truncate

After

CODE:
  1. This is a very long text that I type as an exam....

Here's the PHP code that can let you do just that :

PHP:
  1. function truncate ($str, $length=10, $trailing='...')
  2. {
  3. /*
  4. ** $str -String to truncate
  5. ** $length - length to truncate
  6. ** $trailing - the trailing character, default: "..."
  7. */
  8.       // take off chars for the trailing
  9.       $length-=mb_strlen($trailing);
  10.       if (mb_strlen($str)> $length)
  11.       {
  12.          // string exceeded length, truncate and add trailing dots
  13.          return mb_substr($str,0,$length).$trailing;
  14.       }
  15.       else
  16.       {
  17.          // string was already short enough, return the string
  18.          $res = $str;
  19.       }
  20.  
  21.       return $res;
  22.  
  23. }

Truncate as an excerpt function
Truncate can be use to create excerpts from a very long text to serve as a preview or summary on your PHP application.

Tags: , , , , , ,

Posted in Uncategorized | 1 Comment »

PCRE Cheat Sheet (Perl Compatible Regular Expression)

May 3rd, 2007 by Jon Moffet

Regular Expression (Regex, for short) is a very powerful feature in the PHP programming language. It is useful for manipulating strings using a set of well-defined rules.

However the often complex syntax rules used in Regex can prove to be a challenge to some. Thankfully, the folks at PHPGure has produced a nice and easy to read PCRE Cheat Sheet which contains commonly use Regex syntax like (\w) (\d) (\s) as a source of reference.

Download PCRE (Perl Compatible Regular Expression) Cheat Sheet (pdf) here

p/s: I've personally have the cheat sheet plastered on the wall of my room to remind me of the common regex shortcut (yes, I'm sucks at regex).

Tags: , , , ,

Posted in Uncategorized | 2 Comments »

« Previous Entries Next Entries »