Php is easy to ignore when using explode to split strings

Source: Internet
Author: User
Problems easily overlooked when using explode to split strings in php 1. explode method description

The explode method splits a string into arrays based on a string as a boundary point.

array explode ( string $delimiter , string $string [, int $limit ] )1

Returns an array composed of strings. each element is a substring of a string and is separated by a string delimiter as a boundary point.

Parameter description:
Delimiter
Delimiter on the boundary.

String
Input string

Limit
If the limit parameter is set and it is a positive number, the returned array can contain up to limit elements, and the last element will contain the rest of the string.
If the limit parameter is negative, all elements except the last-limit element are returned.
If the limit value is 0, it is regarded as 1.


2. example

Use to split strings

 ';print_r($arr);echo '
';?> 1234567

Output:

Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5    [5] => 6    [6] => 7    [7] => 8    [8] => 9)123456789101112



Use, split string, limit is positive

 ';print_r($arr);echo '
';?> 1234567

Output:

Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5,6,7,8,9)12345678



Used to split the string. the limit value is negative.

 ';print_r($arr);echo '
';?> 1234567

Output:

Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5    [5] => 6)123456789



Used to split the string. the limit value is 0.

 ';print_r($arr);echo '
';?> 1234567

Output:

Array(    [0] => 1,2,3,4,5,6,7,8,9)1234


3. easy to ignore

In general, we use explode to split data such as id strings.

 $v){        // do sth    }}?>123456789

Normally, ids is not empty, and no problem is found when you look at the code, because if (data) is judged; it is generally considered that null processing has been done.
However, the actual situation is that ids = null, but data is not empty, which may cause problems in the code executed in foreach.

 123456789

Because ids = null, The explode is used for segmentation, and the resulting Array is Array ([0] =>) rather than Array ().

Therefore, you need to modify the settings to avoid problems.

 $v){        // do sth    }}?>

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.