Posts filed under 'PHP'

Converting text to phonetic alphabet

Recently, I had a friend who needed a quick script to take a word, and produce the phonetics for each letter. Didn't take long to knock it up, and thought I'd share...

PHP:
  1. <?php
  2. $chars = array('1'=>'one',
  3. '2'=>'two',
  4. '3'=>'three',
  5. '4'=>'four',
  6. '5'=>'five',
  7. '6'=>'six',
  8. '7'=>'seven',
  9. '8'=>'eight',
  10. '9'=>'nine',
  11. '0'=>'zero',
  12. 'A'=>'capital alpha',
  13. 'B'=>'capital bravo',
  14. 'C'=>'capital charlie',
  15. 'D'=>'capital delta',
  16. 'E'=>'capital echo',
  17. 'F'=>'capital foxtrot',
  18. 'G'=>'capital golf',
  19. 'H'=>'capital hotel',

2 comments February 17th, 2009

Finding the difference between dates

I needed a quick and easy to produce a human readable difference between two dates, and came up with the follwing:

PHP:
  1. <?php
  2. function dateDiff($from,$to) {
  3.   $diff = $to - $from;
  4.   $info = array();
  5.   if($diff>86400) {
  6.     //one or more days
  7.     $info['d'] = ($diff - ($diff%86400))/86400;
  8.     $diff = $diff%86400;
  9.   }
  10.   if($diff>3600) {
  11.     //one or more hours
  12.     $info['h'] = ($diff - ($diff%3600))/3600;
  13.     $diff = $diff%3600;
  14.   }
  15.   if($diff>60) {
  16.     //one or more minutes
  17.     $info['m'] = ($diff - ($diff%60))/60;
  18.     $diff = $diff%60;
  19.   }
  20.   if($diff>0) {
  21.     $info['s'] = $diff;
  22.   }
  23.   $f = '';
  24.   foreach($info as $k=>$v) {
  25.     if($v>0) $f .= "$v $k, ";
  26.   }
  27.   return substr($f,0,-2);
  28. }
  29. ?>

Usage:

Just ensure that you pass it two timestamps, the first one being the date FROM when you want to calculate, the second one being where you want it calculated TO. Tip: You can get the current timestamp by using time().

PHP:
  1. <?php
  2. echo dateDiff(1186233948,1186480322); //2 d, 20 h, 26 m, 14 s
  3. echo dateDiff(1186233948,time()); //3 d, 1 h, 22 m, 7 s
  4. ?>

Enjoy!

3 comments August 7th, 2007

Email Attachments in PHP

I've often been asked how to not just send emails from PHP, but how to send an attachment within the email as well. To send an attachment, you need to add extra headers, base64 encode the file and so on. In fact, it's not very easy to do.

However, there is a script around that makes this a lot easier by handling all the donkey work - PHPMailer. PHPMailer can handle almost anything you want to throw at it, including embedding images within the email, full blown HTML emails, using external SMTP servers to send the emails and so on. However, today, we're going to just concentrate on using PHPMailer to send an email with an attachment.

2 comments April 28th, 2007

httpOnly cookies in PHP

Ilia, one of the PHP developers, has included a patch by Scott MacVicar into CVS for PHP 5.2 that allows you to set a httpOnly cookie instead of a normal cookie. A httpOnly cookie allows you to set a cookie that isn't accessible via Javascript, and so removes the possibility of some nefarious code to try and read your cookie.

This can be enabled in PHP 5.2. by passing TRUE as the 7th paramenter in setcookie() and setrawcookie(). In versions below 5.2, you can set this by passing an additional header.

PHP:
  1. header("Set-Cookie: hidden=value; httpOnly");

Add comment August 11th, 2006

Odd and Even Numbers

Sometimes its necessary to find out whether a number is odd or even. When I initially hit upon the problem, my immediate thought was to use the modulus function % and inspect the result.

However, a quick look at the PHP Manual showed a much easier way of determining whether a number is odd or even:

PHP:
  1. $status = (1 & $number) ? 'Odd' : 'Even';

The '&' sign represents the bitwise 'AND' operator, and the appropriate user comment explains:

4 comments July 13th, 2006

Regular Expressions in PHP

Regular Expressions (regex for short) appear to a lot of people as the 'black art' of coding. Most languages, be it PHP, Java, C, .NET, VB etc have a way of using regular expressions - and they can certainly make your job easier. So lets start on our journey into regular expressions. I am by no means an expert, but hopefully I'll be able to clear the fog that surrounds regular expressions!

Continue Reading 11 comments June 13th, 2006

Determining if a URL exists with Curl

Its quite common for people to enter in their URL when signing up - but what if you want to verify that this is a real page? You can validate the URL using a regular expression up to a point, but all that tells us is that the URL is well formed. What I wanted to do was to check that the page exists - i.e. that we don't get a 404 for it.

Luckily, this is quite easy if you have the Curl extension installed.

Continue Reading 10 comments May 31st, 2006

Using DirectoryIterator to List Files in PHP

During the days of PHP4, the most common way of showing the files in a directory was like so:

PHP:
  1. if ($handle = opendir('/home/fernando/temp')) {
  2.    while (false !== ($file = readdir($handle))) {
  3.        if ($file != "." && $file != "..") {
  4.            print "$file <br />";
  5.        }
  6.    }
  7.    closedir($handle);
  8. }

However, if your PHP version has now been upgraded to PHP 5, you can take advantage of DirectoryIterator instead. This class (don't get scared) is part of the SPL library that is now bundled within PHP 5.

11 comments May 18th, 2006

Installing PEAR

PEAR, the PHP Extension and Application Repository, has been around since 1999, and has several functions. According to the 'About PEAR' page:

The purpose of PEAR is to provide:

  • A structured library of open-sourced code for PHP users
  • A system for code distribution and package maintenance
  • A standard style for code written in PHP
  • The PHP Extension Community Library (PECL)
  • A web site, mailing lists and download mirrors to support the PHP/PEAR community

Getting Started

For most PHP coders, including myself at one point, PEAR seemed to be a bit of a mystery for those who shy away from object orientated programming. The first point to remember is this: you don't need OOP knowledge to use PEAR! Yup, no lies, you can use it if you have an ounce of intelligence, and can read the documentation! So, to calm the fear of PEAR, I'm going to take a step by step approach to installation, and the usage of several PEAR classes in a series of posts.

Continue Reading 2 comments May 10th, 2006

PHP 5.1.4 released

A critical bug with $_POST array handling as well as the FastCGI sapi have been discovered in PHP 5.1.3. A new PHP release 5.1.4 is now available to address these issues. All PHP users are encouraged to upgrade to this release as soon as possible.

Add comment May 5th, 2006

Previous Posts


Calendar

July 2009
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Posts by Month

Posts by Category