[PHP] practical function 2_PHP tutorial-php Tutorial

Source: Internet
Author: User
[PHP] function 2. 1. copy the code to generate a random string function: functionrandom ($ length) {$ hash ##; $ chars # signature ##; $ maxstrl 1. Generate a random string function

The 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. truncate a string of a certain length (this function is valid for GB2312)

The 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

The 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 email address

The 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 are used together)

The 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

The code is as follows:


Mysql_insert_id ();
?>

The compile code is as follows: functionrandom ($ length) {$ hash =##; $ chars =# comment #; $ 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.