PHP Common functions recommended _php tutorial

Source: Internet
Author: User
1. Generating 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. Intercept strings of a certain length

Note: This function is valid for GB2312 use

function Wordscut ($string, $length, $sss =0) {if (strlen ($string) > $length) {               if ($sss) {                      $length = $length -3;< c3/> $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);} 

#P#

4. Create the appropriate folder

function Createdir ($dir = ") {        if (!is_dir ($dir))        {            $temp = explode ('/', $dir);            $cur _dir = ";            for ($i =0; $i
     
     < }="" @mkdir($cur_dir,0777);="" {="" (!is_dir($cur_dir))="" if="" .="$temp[$i].'/';" $cur_dir="">

5. Determine your 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))     
Content= "3;url=". $url. "'" > ";    $html. = "
Type=text/css rel=stylesheet> ";    $html. = "  



";
cellpadding= ' 4 ' width= ' 100% ' "; $html. = "  "; $html. = " 
"; if (!empty ($url)) $html. = "The system will return after 3 seconds
If your browser does not return automatically,
Please click [ here ] enter"; else $html. = "[ return ]"; $html. = "     "; $html. = " "; Echo $html; Exit;}

7. Paging (use with two functions)

$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;    $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."] 
 ":" >>.  "; if ($page <> $totalpage)   
Title= last page > last page  "; return $out;}? >


http://www.bkjia.com/PHPjc/446705.html www.bkjia.com true http://www.bkjia.com/PHPjc/446705.html techarticle 1. Generate random String functions function random ($length) {$hash = '; $chars = ' Abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz '; $max = strlen ($chars)-1; Mt_srand ( ...

  • 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.