-
String
- Get the length of the string: strlen() function
Get the Chinese character length echo mb_strlen($str,”UTF8”);
-
English string interception
<code><span>$str</span>=<span>'i love you'</span>;</code>
Copy after login
//Intercept the letters love
echo substr($str, 2, 4);//Why is the starting position 2? Because the substr function calculates the string position starting from 0, that is, the position of 0 is i, the position of 1 is a space, and the position of l It's 2. Take 4 characters starting from position 2, which is love
Chinese string interception
mb_substr();
- String search
strpos(string to be processed, string to be positioned, starting position of positioning [optional])
- replace string
str_replace(String to find, String to replace, Searched string, Replacement count [optional])
-
Format string
<code><span>$str</span> = <span>'99.9'</span>;</code>
Copy after login
<code><span>$result</span> = <span>sprintf</span>(<span>'%01.2f'</span>, <span>$str</span>);</code>
Copy after login
echo $result;//The result shows 99.90
-
Merge strings
<code><span>$arr</span> = <span>array</span>(<span>'Hello'</span>, <span>'World!'</span>);
<span>$result</span> = implode(<span>''</span>, <span>$arr</span>);
print_r(<span>$result</span>);<span>//结果显示Hello World!</span></code>
Copy after login
-
Split strings
<code><span>`$str` = '</span>apple,banana';
<span>`$result` = explode('</span>,<span>', $str);
print_r($result);//结果显示array('</span>apple',<span>'banana'</span>)</code>
Copy after login
-
String escape function addslashes()
Function description: Used to add escape characters to special characters and return a string
Return 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 is 0, which means it will expire when the browser is closed
path (valid path) If the path is set to '/', the entire website is valid
Domain (valid domain) defaults to the entire domain name being valid. If 'www.imooc.com' is set, it is only valid in the www subdomain
2.
There is also a function setrawcookie in PHP that sets cookies. Setrawcookie is basically the same as setcookie. The only difference is that the value will not be urlencoded automatically, so you need to urlencode it manually when needed
- Delete and set the expiration time
setcookie(‘test’, ”, time()-1);
- Valid path
setcookie('test', time(), 0, '/path');//The path and the subdirectories under it are set to be valid
- session
- Using session in PHP is very simple, first execute session_start The method opens the session, and then reads and writes the session through the global variable$_SESSION.
<br>
session_start(); <br>
$_SESSION['test'] = time(); <br>
var_dump($_SESSION); <br>
- session will automatically encode and decode the value to be set, so session can support any data type, including data and objects.
- Delete
To delete a certain session value, you can use PHP's unset function. After deletion, it will be removed from the global variable$_SESSION and cannot be accessed
<br>
session_start(); <br>
$_SESSION['name'] = 'jobs'; <br>
unset($_SESSION['name']); <br>
echo $_SESSION['name']; //Prompt name does not exist <br>
If you want to delete all sessions, you can use the session_destroy function to destroy the current session. session_destroy will delete all data, but the session_id still exists
session_destroy will not destroy the value in the global variable $_SESSION
immediately. Only when it is accessed next time, $_SESSION
will be empty, so if you need to destroy $_SESSION immediately, you can use the unset function.
If you need to destroy the session_id in the cookie at the same time, which may usually be used when the user logs out, you also need to explicitly call the setcookie method to delete the cookie value of the session_id
').addClass('pre-numbering') .hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i ').text(i));
};
$numbering.fadeIn(1700);
});
});
The above has introduced PHP entry strings, cookies, and sessions, including special characters and global variables. I hope it will be helpful to friends who are interested in PHP tutorials.