1. Generating random String functions
Copy CodeThe code is 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, intercept a certain length of string (this function is valid for GB2312 use)
Copy CodeThe code is 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 the Client IP address
Copy CodeThe code is 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, determine the e-mail address
Copy CodeThe code is 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 of)
Copy CodeThe code is 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 = "Total
". $totalpage."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. = "First page";
if ($start >1)
$out. = ".. << ";
for ($t = $start; $t <= $end; $t + +)
{
$out. = ($page = = $t)? "
[". $t."]":" $t ";
}
if ($end < $totalpage)
$out. = ">>.";
if ($page <> $totalpage)
$out. = "Last page";
return $out;
}
6. Get the ID of the newly inserted data
Copy CodeThe code is as follows:
MYSQL_INSERT_ID ();
?>
http://www.bkjia.com/PHPjc/318390.html www.bkjia.com true http://www.bkjia.com/PHPjc/318390.html techarticle 1. Generate random string function copy code code as follows: Functionrandom ($length) {$hash =@#@#; $chars =@# abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz@#; $max =strl ...