This is a frequently used PHP function.

Source: Internet
Author: User

1. generate random string functions

Function random ($ length ){
$ Hash = ";
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ 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

Note: This function is valid for GB2312.

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

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. Create a folder

Function createdir ($ dir = ")
{
If (! Is_dir ($ dir ))
{
$ Temp = explode ('/, $ dir );
$ Cur_dir = ";
For ($ I = 0; $ I <count ($ temp); $ I ++)
{
$ Cur_dir. = $ temp [$ I]. '/;
If (! Is_dir ($ cur_dir ))
{
@ Mkdir ($ cur_dir, 0777 );
}
}
}
}

5. Determine the email address

Function checkEmail ($ inAddress)
{
Return (ereg ("^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) +", $ inAddress ));

}

6. Jump

Function gotourl ($ message = ", $ url =", $ title = ")
{

$ Html = "If (! Empty ($ url ))
$ Html. = "<meta http-equiv = 'refresh' content =" 3; url = '". $ url."' "> ";
$ Html. = "<link href = '../templates/style.css' type = text/css rel = stylesheet> ";
$ Html. = "$ Html. = "<table cellspacing = '0' cellpadding = '0' border = '1' width = '000000' align = 'center'> ";
$ Html. = "<tr> <td bgcolor = '# ffff'> ";
$ Html. = "<table border = '1' cellspacing = '1' cellpadding = '4' width = '000000'> ";
$ Html. = "<tr class ='m _ title'> ";
$ Html. = "<td>". $ title. "</td> </tr> ";
$ Html. = "<tr class = 'line _ 1'> <td align = 'center' height = '60'> ";
$ Html. = "<br>". $ message. "<br> ";
If (! Empty ($ url ))
$ Html. = "the system will return in 3 seconds <br> If your browser cannot return automatically, click [<a href = ". $ url. "target = _ self> here </a>] Enter ";
Else
$ Html. = "[<a href = # onclick = history. go (-1)> return </a>]";
$ Html. = "</td> </tr> </table> ";
$ Html. = "</body> Echo $ html;
Exit;
}

7. Paging (two functions are used together)

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 <font color = 'red'> <B>". $ totalpage. "</B> </font> page & nbsp ;";
$ 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> & nbsp ;";
If ($ start> 1)
$ Out. = "<a href = '? Page = ". $ prestart." 'title =>... </a> & nbsp ;";
For ($ t = $ start; $ t <= $ end; $ t ++)
{
$ Out. = ($ page = $ t )? "<Font color = 'red'> <B> [". $ t. "] </B> </font> & nbsp;": "<a href = '? Page = $ t & ". $ string." '> $ t </a> & nbsp ;";
}
If ($ end <$ totalpage)
$ Out. = "<a href = '? Page = ". $ nextend." & ". $ string." 'title = >>>... </a> ";
If ($ page <> $ totalpage)
$ Out. = "& nbsp; <a href = '? Page = ". $ totalpage." & ". $ string." 'title = last page> last page </a> ";
Return $ out;
}

?>

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.