Archive

Posts Tagged ‘common’

Some Common Timestamp Intervals

May 2nd, 2008 No comments

Using timestamps is quite a common practise, but converting them into "real life" times can be a little hard. So here are some common time intervals that you might need.

  1. 1: 1 second
  2. 60: 1 minute
  3. 300: 5 minutes
  4. 600: 10 minutes
  5. 2700: 45 minutes
  6. 3600: 1 hour
  7. 54000: 15 hours
  8. 86400: 1 day
  9. 18400: 6 days
  10. 604800: 1 week
  11. 1814400: 3 weeks
  12. 2419200: 1 month
  13. 26611200: 11 months
  14. 14515200: 6 months
  15. 29030400: 1 year
Categories: General Tags: , ,

Common Regular Expressions

March 24th, 2008 No comments

Here are some of the regular expressions that I frequently use.

Find a blank line
^$

Spaces
[ \t]+
You can use this to break a text string apart into words.

Date
\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}
This will match anything in the format mm/dd/yyyy, or even dd/mm/yyyy.
[A-Z][a-z][a-z] [0-9][0-9]*, [0-9]{4}
Will match a formatted date, such as Mar 24, 2007.

Time
([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?
This will match HH:MM or HH:MM:SS or HH:MM:SS.mmm.

IP Address
(((\d{1,2}|(1\d{2})|(2[0-4]\d)|25[0-5]))\.){3}((\d{1,2}|(1\d{2})|(2[0-4]\d)|25[0-5]))
This also checks to see that the IP address is within the range 0.0.0.0 to 255.255.255.255.

Email Address
([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))
This is using a simple mechanism, the following expression uses the RFC standard for an email address format and so should match %99.99 of all email addresses.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Complete URLs
https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?
This will match virtually any URL.

HTML Comments
<!-{2,}.*?-{2,}>

Inline Comments
//.*
This will match inline comments in C, PHP, Java, JavaScript etc.

PHP Array Mode Function

January 7th, 2008 No comments

The following mode function will return the most commonly occurring value from an array of values, also called the mode. If just the array is used then only the most commonly occurring value will be returned. The second parameter can be used to return an array containing the mode and the number of times that this value occurs in the array.
function array_mode($array,$justMode=0) {
  $count = array();
  foreach($array as $item) {
    if(isset($count[$item])){
      $count[$item]++;
    }else{
      $count[$item] = 1;
    };
  };
  $mostcommon = '';
  $iter = 0;
  foreach($count as $k => $v){
    if($v > $iter){
    $mostcommon = $k;
    $iter = $v;
    };
  };
  if($justMode==0){
    return $mostcommon;
  }else{
    return array("mode" => $mostcommon, "count" => $iter);
  }
}

This is used in the following way.
$array = array(1,1,0,1);
print_r(array_mode($array,0)); // prints 1
print_r(array_mode($array,1)); // prints Array ( [mode] => 1 [count] => 3 )

If you just wanted a list of the most commonly occurring values in an array then you could use the PHP function array_count_values(). This will return an array containing all of the values in the array and the number of times each one occurs. Here is an example of it’s use.
$array = array(1,1,0,1);
print_r(array_count_values($array)); // prints Array ( [1] => 3 [0] => 1 )

Categories: PHP Arrays Tags: , , , , ,