Archive

Posts Tagged ‘php function’

PHP function for pagination/paging

May 28th, 2009 admin No comments

Found this function. Not tested yet but maybe useful in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// example variable
$sql_limit = (isset($_GET['limit'])) ? $_GET['limit'] : 0;
 
function navigation_links($curr_limit, $num_records, $limit_val, $limit_var = "limit", $next = "next >", $prev = "< prev", $seperator = "|") {
    // rebuild query string
    if (!empty($_SERVER['QUERY_STRING'])) {
        $parts = explode("&", $_SERVER['QUERY_STRING']);
        $newParts = array();
        foreach ($parts as $val) {
            if (stristr($val, $limit_var) == false) array_push($newParts, $val);
        }
        $qs = (count($newParts) > 0) ? "&".implode("&", $newParts) : "";
    } else {
        $qs = "";
    }
    $navi = "";
    if ($curr_limit > 0) {
        $navi .= "<a href="".$_SERVER['PHP_SELF']."?".$limit_var. "=".($curr_limit-$limit_val).$qs."">".$prev."</a>";
    }
    $navi .= " ".$seperator." ";
    if ($curr_limit < ($num_records-$limit_val)) {
        $navi .= "<a href="".$_SERVER['PHP_SELF']."?".$limit_var. "=".($curr_limit+$limit_val).$qs."">".$next."</a>";
    }
    return trim($navi, " | ");
}
 
// example placing links ($num_all is the value of all records in you result set)
echo navigation_links($sql_limit, $num_all, 10);
?>

source | more functions avail.

PHP code to check email validity

May 21st, 2009 admin No comments

This PHP code can be used to check whether email is valid or not by ILoveJackDaniel. It is a bit lengthy compared to other script, but this one is more comprehensive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function check_email_address($email) {
  // First, we check that there's one @ symbol, 
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
      return false;
    }
  }
  // Check if domain is IP. If not, 
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}