[PHP] Utility function 2_php Tutorial

Source: Internet
Author: User
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. = "&GT;&GT;.";
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.