Get Started with PHP (String, cookie,session), a friend of the need can refer to below. String Gets the length of the string: strlen () function Get Chinese text long echo mb_strlen ($str, "UTF8");English string interception
- $str = ' I love you ';
Copy CodeCapture the letters of love Echo substr ($str, 2, 4);//Why the starting position is 2, because the SUBSTR function calculates that the string position is starting from 0, that is, the position of 0 is the space where the i,1 is, and the position of L is 2. Starting from position 2, take 4 characters, that is love Chinese string interception Mb_substr (); String Lookup Strpos (string to be processed, string to locate, start position of anchor [optional]) Replacement string Str_replace (The string to find, the string to replace, the string to be searched, and the replacement to count [optional])formatting strings
- $STR = ' 99.9 ';
Copy Code
- $result = sprintf ('%01.2f ', $str);
Copy Codeecho $result;//Results show 99.90 Merging strings
- $arr = Array (' Hello ', ' world! ');
- $result = Implode (' ', $arr);
- Print_r ($result);//results show Hello world!
Copy CodeSplit string
- ' $str ' = ' apple,banana ';
- ' $result ' = explode (', ', $str);
- Print_r ($result);//Results Display array (' Apple ', ' banana ')
Copy CodeString escape function Addslashes () Function Description: Used to add an escape character to a special character, returns a string Return value: An escaped string Example: $str = "What ' s your name?"; echo addslashes ($STR);//output: what\ ' s your name Cookies Common parameters Name (COOKIE name) can be accessed by $_cookie[' name ') Value (values of the cookie) Expire (expiry time) UNIX timestamp format, default = 0, indicates that the browser is off and is invalidated Path (valid path) if the path is set to '/', the entire site is valid Domain (valid domain) default entire domain name is valid, if ' www.imooc.com ' is set, it is only valid in WWW subdomain 2. PHP also has a function to set the cookie Setrawcookie,setrawcookie is basically the same as Setcookie, the only difference is that value values are not automatically urlencode, So when you need to manually do UrlEncode delete and set the expiration time Setcookie (' Test ', ", Time ()-1); Valid path Setcookie (' Test ', Time (), 0, '/path ');//Set the path and its sub-directory valid session Using the session in PHP is very simple, first executing the Session_Start method to open the session, and then through the global variable $_session session read and write. Session_Start (); $_session[' Test ' = time (); Var_dump ($_session); The session automatically encode and decode the values to be set, so the session can support any data type, including data and objects. Delete Deleting a session value can use PHP's unset function, which is removed from the global variable $_session and cannot be accessed Session_Start (); $_session[' name '] = ' jobs '; unset ($_session[' name '); echo $_session[' name ']; Hint name does not exist If you want to delete all sessions, you can use the Session_destroy function to destroy the current Session,session_destroy delete all data, but session_id still exists Session_destroy does not immediately destroy the value in the global variable $_session, and the $_session is empty only the next time it is accessed, so you can use the unset function if you need to destroy $_session immediately. If it is necessary to destroy the session_id in the cookie at the same time, usually when the user exits, you also need to explicitly call the Setcookie method to delete the session_id cookie value. |