PHP string Function Tutorial: PHP string Split function processing

Source: Internet
Author: User
Tags explode file upload function prototype functions php file split string back strtok

PHP String functions are widely used in the development of PHP Web sites, such as the use of PHP string functions for string segmentation, interception, matching, replacement and so on. PHP String functions are essential for beginners in PHP, this article will mainly introduce PHP string segmentation function processing experience, to open the PHP string function introductory learning tour.

  Common PHP String Partitioning function

The most commonly used PHP split string functions are explode, strtok, Str_split, which are mainly used to split strings and return as arrays or strings, corresponding to these three PHP string-separated functions, The PHP string function that splits the string with the delimiter connection has implode, joins, the effect is the opposite of explode, and the join function is the alias of the implode function.

  PHP string partition function explode processing instructions

Function prototype: array explode (string separator,string input);

The explode function is widely used, and its main function is to split the specified string into a set delimiter and return it as an array. It is often used in the split file name to determine the file type, cutting user email and other occasions.

  Explode processing instance of PHP string partition function

1, get the file name 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 judge whether the file name is legitimate is to determine whether the extension is legitimate, this time you need to use the PHP string function explode to the file name segmentation process. In the above code, explode the function to split the file name into a delimiter. Enter the results as follows

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

2, get the user email domain name information

1
$emailInfo = Explode ("@", $email);

3, get user access to the URL specific file name

1
2
$url = "http://www.leapsoul.cn/index.php";
$urlFileName = Explode ("/", $url);

Output results

1
Array ([0] => http: [1] => [2] => www.leapsoul.cn [3] => index.php)

  PHP string partition function Strtok processing instructions

Function prototype: string strtok (string input,string separator);

The difference between the PHP string function strtok and the explode function is that the strtok function, after dividing the string, remembers the position of the new string in the original string in order to continue the split, and the return type is string. If you want to split again, just pass the string back to Strtok.

  Strtok processing instance of PHP string partition function

Split URL address for user access

1
2
3
4
5
6
7
8
9
$url = "http://www.leapsoul.cn/index.php";
$urlFileName = Strtok ($url, "/");
echo $urlFileName. " <br/> ";

while (!empty ($urlFileName))
{
$urlFileName = Strtok ("/");
echo $urlFileName. " <br/> ";
}

Output results

1
2
3
http
www.leapsoul.cn
index.php

  PHP string partition function Str_split processing instructions

Function prototypes: Array str_split (STRING,LENGTH)

length defaults to 1 and returns false if length is less than 1, and returns the entire string as an array element if length is greater than the original length of the string.

The difference between the PHP string function str_split and the explode function is that str_split splits the string in length rather than the delimiter, somewhat analogous to the way substr string functions are handled.

  Summary of PHP string Split function

Relatively speaking, PHP string partition function explode widely used, combined with PHP string matching, interception function can make a lot of applications, my php file upload function and weather plug-ins are applied to the processing of PHP string functions.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







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.