php string function is essential, give us a lot of convenience to solve practical problems, such as the use of PHP string function string segmentation, interception, matching, replacement and other treatment. PHP string function is essential for PHP entry learners, so we will mainly introduce PHP string segmentation function processing experience, open the PHP string function Getting Started tutorial tour.
Commonly used PHP string segmentation function
Commonly used PHP split string functions are explode, strtok, str_split, mainly used to split the string and return as an array or string, and the three PHP string separation function corresponds to the separator connection split The substring PHP string functions implode, join, the effect and explode the opposite, the join function is an alias of the implode function.
PHP string split function explode processing instructions
Prototype: array explode (string separator, string input);
Explode function is widely used, the main role is to set the specified string delimiter split and returned as an array. It is often used in the file name to determine the file type, cut the user Email and other occasions.
PHP string split function explode processing instance
1, get the file extension
$ fileName = "leapsoulcn.jpg";
$ str = explode (".", $ fileName);
print_r ($ str);
We know that in the PHP file upload function, the most basic method to determine whether the uploaded file name is valid is to determine whether the extension is valid, this time you need to use the PHP string function explode file name segmentation. In the above code explode function as a delimiter, the file name segmentation. Enter the following results
Array ([0] => leapsoulcn [1] => jpg)
2, access to user Email domain name information
$ emailInfo = explode ("@", $ email);
3, access to the user to access the URL specific file name
$ url = "http://www.jzread.com/index.php";
$ urlFileName = explode ("/", $ url);
Output the result
Array ([0] => http: [1] => [2] => www.jzread.com [3] => index.php)
PHP string segmentation function strtok processing instructions
Prototype: string strtok (string input, string separator);
PHP string function strtok and explode function difference is that the strtok function after splitting the string can remember the new split string position in the original string in order to continue the split, the return type is string. If you want to re-split, as long as the string can be re-transmitted to strtok.
PHP string segmentation function strtok deal with examples
Split user access to the URL address
$ url = "http://www.jzread.com/index.html";
$ urlFileName = strtok ($ url, "/");
echo $ urlFileName. "";
while (! empty ($ urlFileName))
{
$ urlFileName = strtok ("/");
echo $ urlFileName. "";
}
Output the result
http:
www.jzread.com
index.php
PHP string segmentation function str_split processing instructions
Prototype: array str_split (string, length)
length defaults to 1, false if length is less than 1, and the entire string as an array element if length is greater than the original length of the string.
The difference between the str_split PHP function and the explode function is that str_split splits the string in length instead of delimiter, which is somewhat similar to how substr string functions are handled.
PHP string segmentation function summary
Relatively speaking, PHP string segmentation function explode more widely used, combined with PHP string matching, interception function can make a lot of applications, my PHP file upload function and weather forecast plugin are applied to the PHP string function.