PHP using explode to split the string novice easy to ignore problems

Source: Internet
Author: User

1.explode Method Description

The Explode method allows a string to be split into groups by one string as a boundary point.

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

Returns an array of strings, each of which is a substring of string, separated by the delimiter as the boundary point.

parameter Description:
Delimiter
The delimited character on the boundary.

String
The input string

Limit
If the limit parameter is set and is a positive number, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string.
If the limit parameter is a negative number, all elements except the last-limit element are returned.
If limit is 0, it will be treated as 1.


2. Example

Use, split string

 
  ';p rint_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

 
  ';p rint_r ($arr); Echo '
';? >1234567

Output:

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



Use, split string, negative limit

 
  ';p rint_r ($arr); Echo '
';? >1234567

Output:

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



Use, split string, limit is 0

 
  ';p rint_r ($arr); Echo '
';? >1234567

Output:

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


3. Easy to ignore problems

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

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

Under normal circumstances, IDS is not empty, look at the code will not find any problem, because there is a judgment if (data), it is generally considered to have done an empty processing.
But the reality is ids=null, but data is not empty, causing problems with the code executing in foreach.

 
  123456789

Because Ids=null, the array ([0] = =) instead of array () is derived using explode.

So the judgment needs to be modified 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.