- function Encryptdecrypt ($key, $string, $decrypt) {
- if ($decrypt) {
- $decrypted = RTrim (Mcrypt_decrypt (mcrypt_rijndael_256, MD5 ($key), Base64_decode ($string), MCRYPT_MODE_CBC, MD5 (MD5 ($ Key)), "12");
- return $decrypted;
- }else{
- $encrypted = Base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, MD5 ($key), $string, MCRYPT_MODE_CBC, MD5 (MD5 ($KEY)))) ;
- return $encrypted;
- }
- }
Copy CodeHow to use:
- Here's how to encrypt and decrypt the string "Helloweba welcome You" separately
- Encryption:
- echo encryptdecrypt (' Password ', ' Helloweba Welcome ', 0);
- Decrypt:
- echo encryptdecrypt (' Password ', ' z0jax4qmwcf+db5tnbp/xwdum84snrsxvvpxuaca4bk= ', 1);
Copy Code2. PHP generates random string Use the following function when you need to generate a string such as a random name, temporary password, and so on:
- function generaterandomstring ($length = 10) {
- $characters = ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';
- $randomstring = ";
- for ($i = 0; $i < $length; $i + +) {
- $randomstring. = $characters [rand (0, strlen ($characters)-1)];
- }
- return $randomstring;
- }
Copy CodeHow to use:
- Echo generaterandomstring (20);
Copy Code3, php get file extension (suffix) quick get file extension is the suffix.
- function GetExtension ($filename) {
- $myext = substr ($filename, Strrpos ($filename, '. '));
- Return Str_replace ('. ', ', $myext);
- }
Copy CodeHow to use:
- $filename = ' My Documents. Doc ';
- echo getextension ($filename);
Copy Code4, PHP to get the file size and format to get the size of the file, and converted into easy-to-read KB,MB and other formats.
- function Formatsize ($size) {
- $sizes = Array ("bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
- if ($size = = 0) {
- Return (' N/a ');
- } else {
- Return (Round ($size/pow ($i = Floor (log ($size, 1024x768))), 2). $sizes [$i]);
- }
- }
Copy CodeHow to use:
- $thefile = filesize (' Test_file.mp3 ');
- echo formatsize ($thefile);
Copy Code5. The PHP replacement tag character replaces the string, template tag with the specified content, function:
- function Stringparser ($string, $replacer) {
- $result = Str_replace (Array_keys ($replacer), Array_values ($replacer), $string);
- return $result;
- }
Copy CodeHow to use:
$string = ' The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself ';
- $replace _array = Array (' {b} ' = = ', ' {/b} '= ', ' {br} ' = ')
');
Echo Stringparser ($string, $replace _array);
Copy Code6. The file name under PHP list directory lists all the files in the directory:
- function Listdirfiles ($dirpath) {
- if ($dir = Opendir ($dirpath)) {
- while (($file = Readdir ($dir))!== false) {
- if (!is_dir ($dirpath. $file))
- {
- echo "FileName: $file
";
- }
- }
- }
- }
Copy CodeMethod of Use:listdirfiles (' home/some_folder/'); 7, PHP gets the current page URL The following function can get the URL of the current page, whether HTTP or HTTPS.
- function Curpageurl () {
- $pageurl = ' http ';
- if (!empty ($_server[' https ')) {$pageurl. = "S";}
- $pageurl. = "://";
- if ($_server["Server_port"]! = "80") {
- $pageurl. = $_server["SERVER_NAME"]. ":". $_server["Server_port"].$_server["Request_uri"];
- } else {
- $pageurl. = $_server["SERVER_NAME"].$_server["Request_uri"];
- }
- return $pageurl;
- }
Copy CodeHow to use:
- Echo Curpageurl ();
Copy Code8, php force download file does not want to let the browser directly open files, such as PDF files, but to directly download files, then the following function can force download files, the function is used Application/octet-stream header type.
- function Download ($filename) {
- if ((Isset ($filename)) && (File_exists ($filename))) {
- Header ("Content-length:". FileSize ($filename));
- Header (' Content-type:application/octet-stream ');
- Header (' content-disposition:attachment; Filename= '. $filename. '"');
- ReadFile ("$filename");
- } else {
- echo "Looks like file does not exist!";
- }
- }
Copy CodeHow to use:
- Download ('/down/test_45f73e852.zip ');
Copy Code9, PHP intercept string length needs to intercept strings (including Chinese characters) length, such as the title display can not exceed the number of characters, the length of the ... Indicates that the following functions are available to meet your needs.
/*
- Chinese character interception function supported by Utf-8 and gb2312
- Cut_str (string, intercept length, start length, encode);
- encoding defaults to Utf-8
- Start length defaults to 0
- */
- function Cutstr ($string, $sublen, $start = 0, $code = ' utf-8 ') {
- if ($code = = ' Utf-8 ') {
- $pa = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]| [\xe1-\xef] [\X80-\XBF] [\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]| [\xf1-\xf7] [\X80-\XBF] [\X80-\XBF] [\x80-\xbf]/];
- Preg_match_all ($pa, $string, $t _string);
if (count ($t _string[0])-$start > $sublen) return join ("', Array_slice ($t _string[0], $start, $sublen))." ...";
- return join ("', Array_slice ($t _string[0], $start, $sublen));
- }else{
- $start = $start;
- $sublen = $sublen;
- $strlen = strlen ($string);
- $tmpstr = ";
for ($i =0; $i < $strlen; $i + +) {
- if ($i >= $start && $i < ($start + $sublen)) {
- if (Ord (substr ($string, $i, 1)) >129) {
- $tmpstr. = substr ($string, $i, 2);
- }else{
- $tmpstr. = substr ($string, $i, 1);
- }
- }
- if (Ord (substr ($string, $i, 1)) >129) $i + +;
- }
- if (strlen ($TMPSTR) < $strlen) $tmpstr. = "...";
- return $tmpstr;
- }
- }
Copy CodeHow to use:
- $STR = "Load images and page effects implemented by jquery plug-ins";
- Echo Cutstr ($STR, 16);
Copy Code10, PHP to obtain the client real IP often use the database to record the user's IP, to obtain the client's real IP:
- Get the user's real IP
- 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);
- }
Copy CodeHow to use:
- echo GetIP ();
Copy Code11, PHP to prevent SQL injection when querying the database, for security reasons, you need to filter some illegal characters to prevent SQL malicious injection, please look at the function:
- function Injcheck ($sql _str) {
- $check = Preg_match ('/select|insert|update|delete|\ ' |\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/', $sql _str );
- if ($check) {
- echo ' Illegal characters!! ';
- Exit
- } else {
- return $sql _str;
- }
- }
Copy CodeHere's how to use it:
- Function message ($msgtitle, $message, $jumpurl) {
- $str = ";
- $str. = ';
- $str. = ';
- $str. = ' ;
- $str. = 'Page Tips';
- $str. = ' ;
- $str. = ';
- $str. = ';
- $str. = ';
- $str. = '
'. $msgtitle. ' ';
- $str. = ';
- $str. = '
'. $message. ' ';
- $str. = '
The system will automatically jump after 3 seconds, if you do not want to wait, click here to jump ';
- $str. = "";
- $str. = ';
- $str. = ';
- $str. = ';
- $str. = ';
- Echo $str;
- }
Copy CodeHow to use:
- function Changetimetype ($seconds) {
- if ($seconds > 3600) {
- $hours = intval ($seconds/3600);
- $minutes = $seconds% 3600;
- $time = $hours. ":" . Gmstrftime ('%m:%s ', $minutes);
- } else {
- $time = Gmstrftime ('%h:%m:%s ', $seconds);
- }
- return $time;
- }
Copy CodeHow to use: $seconds = 3712; echo Changetimetype ($seconds); |