Php entry (string, cookie, session). If you need it, refer. String Obtain the string length: strlen () function Obtain the text length echo mb_strlen ($ str, "UTF8 ");English string truncation
- $ Str = 'I love you ';
// Intercept the letters love. Echo substr ($ str, 2, 4); // why is the start position 2? because the position of the string calculated by the substr function starts from 0, that is, the position 0 is I, the position of 1 is a space, and the position of l is 2. Start from position 2 and take 4 characters, that is, love. Chinese string truncation Mb_substr (); String search Strpos (the string to be processed, the string to be located, the starting position of the positioning [optional]) to replace the string Str_replace (string to be searched, string to be replaced, string to be searched, replacement for counting [optional])Format a string
- $ Str = '99. 9 ';
- $ Result = sprintf ('% 01.2f', $ str );
Echo $ result; // The result is 99.90. Merge strings
- $ Arr = array ('hello', 'World! ');
- $ Result = implode ('', $ arr );
- Print_r ($ result); // The result shows Hello World!
Split string
- '$ Str' = 'Apple, banana ';
- '$ Result' = explode (', ', $ str );
- Print_r ($ result); // The result shows array ('apple', 'banana ')
String escape function addslashes () Function description: used to add escape characters to special characters and return a string. Returned value: an escaped string. Example: $ Str = "what's your name ?"; Echo addslashes ($ str); // output: what \'s your name Cookie Common parameters Name (Cookie name) can be accessed through $ _ COOKIE ['name '] Value (Cookie value) Expire (Expiration Time) Unix timestamp format. the default value is 0, indicating that the browser is disabled or becomes invalid. Path (valid path) if the path is set to '/', the entire website is valid. Domain (valid domain) is valid for the entire domain name by default. if 'www .imooc.com 'is set, it is valid only in the www subdomain. 2. There is also a function setrawcookie in PHP to set cookies. setrawcookie is basically the same as setcookie. The only difference is that the value does not automatically perform urlencode, therefore, you need to manually delete urlencode and set the expiration time when necessary. Setcookie ('test', ", time ()-1); valid path Setcookie ('test', time (), 0, '/path'); // you can specify a valid session for the path and its subdirectories. It is very easy to use session in PHP. first, execute the session_start method to enable the session, and then use the global variable $ _ SESSION to read and write the session. Session_start (); $ _ SESSION ['test'] = time (); Var_dump ($ _ SESSION ); The session automatically performs encode and decode on the value to be set. Therefore, the session can support any data type, including data and objects. Delete You can use the PHP unset function to delete a session value. after deletion, it will be removed from the global variable $ _ SESSION and cannot be accessed. Session_start (); $ _ SESSION ['name'] = 'job '; Unset ($ _ SESSION ['name']); Echo $ _ SESSION ['name']; // the system prompts that the 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 deletes all data, but session_id still exists. Session_destroy does not immediately destroy the value in the global variable $ _ SESSION. it is null only when the next access is made. Therefore, if you need to destroy $ _ SESSION immediately, you can use the unset function. If you need to destroy session_id in the cookie at the same time, it is usually used when the user exits, you also need to explicitly call the setcookie method to delete the cookie value of session_id |