Some very useful PHP functions that use a high frequency

Source: Internet
Author: User
Tags count empty hash strlen

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 a certain length of string

Note: This function is valid for GB2312 use

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) {
    $wor Dscut. = $string [$i]. $string [$i + 1];
    $i + +;
  } else {
    $wordscut. = $string [$i];
  }
 }
  return $wordscut. $ADDSTR
&NBSP}
 return $string;
}

3. Obtain 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 the appropriate 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=@ #450 @# align=@ #center @#>";
$html. = "<tr><td bgcolor=@# #ffffff @#>";
$html. = "<table border=@ #1 @# cellspacing=@ #1 @# cellpadding=@ #4 @# width=@ #100%@#>";
$html. = "<tr class=@ #m_title @#>";
$html. = "<td>". $title. " </td></tr> ";
$html. = "<trclass=@ #line_1 @#><td align=@ #center @# height=@ #60 @#> ";
$html. = "<br>". $message. " <br><br> ";
if (!empty ($url))
$html. = "System will return in 3 seconds <br> If your browser does not return automatically, please click [<a href=". $url. "Target=_self> here </a>] enter";
Else
$html. = "[<a href=@##@# onclick=@ #history. Go ( -1) @#> return </a>]";
$html. = "</td></tr></table></td></tr></table>";
$html. = "</body>Echo $html;
Exit
}

7. Paging (two functions 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 = "Altogether <font". $totalpage. "". $totalpage."color=@ #red @#><b> ". $totalpage." </b></font> 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. = "<a href=@#?page=1&&". $string. " @ #title = First page > first page </a> ";
if ($start >1)
$out. = "<a href=@#?page=". $prestart. " @# title= Previous Page "... <<</a> ";
for ($t = $start; $t <= $end; $t + +)
{
$out. = ($page = = $t)? "<font [". $t. "] color=@ #red @#><b>[". $t."] </b></font> ":" <a $t$thref =@#?page= $t && ". $string." @#> $t </a> ";
}
if ($end < $totalpage)
$out. = "<a href=@#?page=". $nextend. " && ". $string." @# title= next page >>> </a> ";
if ($page <> $totalpage)
$out. = "<a href=@#?page=". $totalpage. " && ". $string." @# title= last page > last page </a> ";
return $out;
}

8. Get the ID of the newly inserted data

?
MYSQL_INSERT_ID ();
?>



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.