PHP string functions are widely used in PHP website development. For example, PHP string functions are used to segment, intercept, match, and replace strings. PHP string functions are essential for beginners of PHP. This article describes how to process PHP string functions and how to start PHP string functions.
Common PHP string segmentation Functions
Common PHP string separation functions include explode, strtok, and str_split. They are mainly used to split strings and return results in arrays or strings. They correspond to the separation functions of these three PHP Strings, the PHP string functions that use delimiters to connect and split strings include implode and join. The results are the opposite of explode. In addition, the join function is the alias of the implode function.
PHP string segmentation function explode processing instructions
Function prototype: array explode (string separator, string input );
The explode function is widely used. Its main function is to split the specified string with a set separator and return it as an array. It is often used to split file names to determine the file type, cut user email, and other occasions.
PHP string splitting 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 way to determine whether the file name to be uploaded is legal is to determine whether the extension is legal. In this case, we need to use the PHP string function explode to split the file name. In the above Code, the explode function uses the. separator to separate file names. The input result is as follows:
Array ([0] => leapsoulcn [1] => JPG)
2. Get user email domain name information
$ Emailinfo = explode ("@", $ email );
3. Get the specific file name of the URL accessed by the user
$ Url = "http://www.leapsoul.cn/index.php ";
$ Urlfilename = explode ("/", $ URL );
Output result
Array ([0] => HTTP: [1] => [2] => www.leapsoul.cn [3] => index. php)
Strtok
Function prototype: String strtok (string input, string separator );
The difference between the PHP string function strtok and the explode function is that after the strtok function splits the string, you can remember the position of the new string in the original string to facilitate further segmentation. The return type is string. If you want to re-split the string, you just need to re-pass it to strtok.
PHP string splitting function strtok processing instance
Split the URL of user access
$ Url = "http://www.onuu.net/index.php ";
$ Urlfilename = strtok ($ URL ,"/");
Echo $ urlfilename. "<br/> ";
While (! Empty ($ urlfilename ))
{
$ Urlfilename = strtok ("/");
Echo $ urlfilename. "<br/> ";
}
Output result
HTTP:
Www.onuu.net
Index. php
PHP string splitting function str_split processing description
Function prototype: array str_split (string, length)
The default length is 1. If the length is smaller than 1, false is returned. If the length is greater than the original length of the string, the entire string is returned as an array element.
The difference between the str_split and explode functions of PHP string functions is that str_split splits strings by length rather than by separator, which is somewhat similar to the processing method of substr string functions.
PHP string segmentation function Summary
PHP string segmentation function explode is widely used. It can be used in combination with PHP string matching and truncation functions. My PHP file upload function and weather forecast plug-in are applied to PhP string functions.