1. Generating random String functions
Copy Code code as follows:
function Random ($length) {
$hash = @#@#;
$chars = @ #ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz @#;
$max = strlen ($chars)-1;
Mt_srand (Double) microtime () * 1000000);
for ($i = 0; $i < $length; $i + +) {
$hash. = $chars [Mt_rand (0, $max)];
}
return $hash;
}
2, the interception of a certain length of the string (the function is valid for GB2312 use)
Copy Code code as follows:
function Wordscut ($string, $length, $sss =0) {
if (strlen ($string) > $length) {
if ($sss) {
$length = $length-3;
$addstr =@# ... @#;
}
for ($i = 0; $i < $length; $i + +) {
if (Ord ($string [$i]) > 127) {
$wordscut. = $string [$i]. $string [$i + 1];
$i + +;
} else {
$wordscut. = $string [$i];
}
}
return $wordscut. $addstr;
}
return $string;
}
3. Obtain Client IP Address
Copy Code code as follows:
function GetIP () {
if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "Unknown")
$ip = getenv ("Http_client_ip");
else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown")
$ip = getenv ("Http_x_forwarded_for");
else if (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "Unknown")
$ip = getenv ("REMOTE_ADDR");
else if (isset ($_server[@ #REMOTE_ADDR @#]) && $_server[@ #REMOTE_ADDR @#] && strcasecmp ($_server[@# remote_addr@#], "unknown")
$ip = $_server[@ #REMOTE_ADDR @#];
Else
$ip = "Unknown";
return ($IP);
}
4, to determine the e-mail address
Copy Code code as follows:
function Checkemail ($inAddress)
{
Return (Ereg ("^ ([a-za-z0-9_-]) +@" ([a-za-z0-9_-]) + (\.[ A-za-z0-9_-]) + ", $inAddress));
}
5, paging (two functions with the use)
Copy Code code as follows:
function GetPage ($sql, $page _size=20)
{
Global $page, $totalpage, $sums; Out param
$page = $_get["page"];
$eachpage = $page _size;
$pagesql = Strstr ($sql, "from");
$pagesql = "SELECT count (*) as IDs". $pagesql;
$result = mysql_query ($pagesql);
if ($rs = mysql_fetch_array ($result)) $sums = $rs [0];
$totalpage = Ceil ($sums/$page _size);
if ((! $page) ($page <1)) $page = 1;
$startpos = ($page-1) * $page _size;
$sql. = "Limit $startpos, $page _size";
return $sql;
}
function Showbar ($string = "")
{
Global $page, $totalpage;
$out = "Altogether <font". $totalpage. " color=@ #red @#><b> ". $totalpage." </b></font> page ";
$linkNum = 4;
$start = ($page-round ($linkNum/2)) >0? ($page-round ($linkNum/2)): "1";
$end = ($page +round ($linkNum/2)) < $totalpage? ($page +round ($linkNum/2)): $totalpage;
$prestart = $start-1;
$nextend = $end +1;
if ($page <>1)
$out. = "<a href=@#?page=1&&". $string. " @ #title = First page > first page </a> ";
if ($start >1)
$out. = "<a href=@#?page=". $prestart. " @# title= Previous Page "... <<</a> ";
for ($t = $start; $t <= $end; $t + +)
{
$out. = ($page = = $t)? "<font [". $t. "] color=@ #red @#><b>[". $t."] </b></font> ":" <a $thref =@#?page= $t && ". $string." @#> $t </a> ";
}
if ($end < $totalpage)
$out. = "<a href=@#?page=". $nextend. " && ". $string." @# title= next page >>> </a> ";
if ($page <> $totalpage)
$out. = "<a href=@#?page=". $totalpage. " && ". $string." @# title= last page > last page </a> ";
return $out;
}
6, get the ID of the newly inserted data
Copy Code code as follows: