Some very useful PHP functions with high application frequency. generate a random string function 2. truncate a string of a certain length 3. obtain the client IP address 4. create a folder. determining the email address 7. paging (two functions work with the application) 8. obtain the ID of the newly inserted data
1. Generate random string functions
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
Note: This function is valid for GB2312 applications.
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 {
$ 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. =' ';
$ Html. =' ';
$ Html. ='
';
$ Html. ='
';$ Html. ='
'; $ Html. ='
';$ Html. ='
';$ Html. ='
'. $ Title .' |
';$ Html. ='
'; $ Html. =' '. $ Message .'
'; If (! Empty ($ url )) $ Html. = 'The system will return in 3 seconds If your browser cannot return the result, click [here] to enter '; Else $ Html. = '[return]'; $ Html. =' |
|
';
$ Html. ='';
Echo $ html;
Exit;
}
7. paging (two functions work with the application)
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 .'Color = @ # red @ #>'. $ 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. = 'Page 1 ';
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;
}
8. get the ID of the newly inserted data
Mysql_insert_id ();
?>