PHP string function tutorial: PHP string segmentation function processing

Source: Internet
Author: User
Tags php website strtok
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 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

1
2
3
$ 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:

1
Array ([0] => leapsoulcn [1] => jpg)

2. get user Email domain name information

1
$ EmailInfo = explode ("@", $ email );

3. get the specific file name of the URL accessed by the user

1
2
$ Url = "http://www.leapsoul.cn/index.php ";
$ UrlFileName = explode ("/", $ url );

Output result

1
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

1
2
3
4
5
6
7
8
9
$ Url = "http://www.leapsoul.cn/index.php ";
$ UrlFileName = strtok ($ url ,"/");
Echo $ urlFileName ."
";

While (! Empty ($ urlFileName ))
{
$ UrlFileName = strtok ("/");
Echo $ urlFileName ."
";
}

Output result

1
2
3
Http:
Www.leapsoul.cn
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.