E-Mail sample source codes

Get Email Addresses from Strings

<?php

  function get_emails ($str)
  {
    $emails = array();
    preg_match_all(“/\b\w+\@\w+[\.\w+]+\b/”, $str, $output);
    foreach($output[0] as $email) array_push ($emails, strtolower($email));
    if (count ($emails) >= 1) return $emails;
    else return false;
  }
  
  # Here is how to use it.
    
  # Sample string containing email addresses;
  $str = “test test@test.com ha ha heHe@test.com bla bla bla@test.com“;
  
  # Get the emails on arrays;
  $emails = get_emails ($str);
  
  # Print that arrays;
  print_r ($emails);
?>

Create an Array from SQL Result

Create an Array from SQL Result

<?
/*
* nas_mysql_result — connect to mysql then load query result
*                     into a two dimensional array.
* —–
*/
function nas_mysql_result($dbhost,$dbuserlogin,$dbpassword,$dbname,$dbsql,$rstype) {
  $dbconn = mysql_connect($dbhost,$dbuserlogin,$dbpassword) or die(“Server
Unavailable”);
  mysql_select_db($dbname,$dbconn) or die(“Database Unavailable”);
  $result = mysql_query($dbsql) or die(“Query Unavailable”);

  //– initial value –
  $iRows = 0;
  $iCols = 0;

  $iRows = mysql_num_rows($result);
  $iCols = mysql_num_fields($result);
  settype($arrContents,”array”);

  switch ($rstype) {
    case “byindex”:
      for($row=0; $row<$iRows; $row++){
        $rs = mysql_fetch_row($result);
        for($col=0; $col<$iCols; $col++){
          $arrContents[$row][$col] = $rs[$col];
        };
      };
      return $arrContents;
      break;
    case “byname”:
      for($row=0; $row<$iRows; $row++){
        $rs = mysql_fetch_row($result);
        for($col=0; $col<$iCols; $col++){
          $arrContents[$row][mysql_field_name($result,$col)] = $rs[$col];
        };
      };
      return $arrContents;
      break;
  };

  mysql_free_result($result);
  mysql_close($dbconn);
};
?>

Validation function for LUHNMod10 and variant. Can discriminate credit card

Validation function for LUHNMod10 and variant. Can discriminate credit card
numbers of varying lengths.

<?
function prestoLUHN($ccn=”",$alt=”0″)
{    
// pre-validations
$ccn = ereg_replace(“[^[:digit:]]+”, “”, $ccn);
if ((strlen($ccn) < 1)) return FALSE;

// Mapping: Double-then-SumOfDigits  (0123456789) => (0246813579)
for($i=0;$i<=9;$i++) $v[$i] = (($i*2)<9) ? ($i*2):($i*2)-9;

// Initial value of $alt (Alternation Pattern) determines which digits are transformed by $v[] during checksum calculation
// 0 = apply transform to even-numbered digits from LSD-to-MSD (Conventional LUHNMod10)
// 1 = apply transform to  odd-numbered digits from LSD-to-MSD (alternative)

$checksum = 0;     
// Calculate checksum from the end of the string (the low-order digit) to beginning of string
for($i = strlen($ccn)-1; $i >= 0; $i–)
{
   $digit = substr($ccn, $i, 1);
   $checksum += $alt ? $v[$digit] : $digit;
   $alt ^= 1;
}

// return the checksum descrimination result
return !($checksum % 10);      
};         
?>

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Posted in PHP. 1 Comment »
Follow

Get every new post delivered to your Inbox.