Php string splitting function

Source: Internet
Author: User
In php, two common functions are used to split strings: chunk_split and explode. There is also a str_split function. here is the example. Define and use the chunk_split () function

In php, two common functions are used to split strings: chunk_split and explode. There is also a str_split function. here is the example.

Definition and usage

The chunk_split () function splits a string into a series of smaller parts.

Syntax:Chunk_split (string, length, end)

Parameters:String is required, which specifies the string to be split.

Parameters:Length (optional) is a number that defines the length of a string block.

Parameters:End (optional) string value, which defines the content placed after each string block.

  1. */
  2. $ Data = "hello world! This is a world! "; // Define a string
  3. $ New_string = chunk_split ($ data); // splits the string
  4. Echo $ new_string; // output result
  5. /*

Definition and usage

The explode () function separates strings into arrays.

Syntax:Explode (separator, string, limit)

Parameters:The separator is required to specify where to split the string.

Parameters:String is required. the string to be split.

Parameters:Limit (optional) specifies the maximum number of returned array elements.

  1. */
  2. $ Str = 'one | two | three | four '; // define a string
  3. $ Result = explode ('|', $ str, 2); // cut the string
  4. Print_r ($ result); // output result
  5. $ Result = explode ('|', $ str,-1); // return the number with a negative number.
  6. Print_r ($ result); // output result
  7. /*

Definition and usage

The str_split () function separates strings into arrays.

Syntax:Str_split (string, length)

Parameters:String is required, which specifies the string to be split.

Parameters:Length (optional) specifies the length of each array element. the default value is 1.

  1. $ Str = "hello world"; // define a string
  2. $ Result = str_split ($ str); // perform the conversion operation.
  3. Print_r ($ result); // output the converted result
  4. $ Result = str_split ($ str, 4); // Each element is set to 4
  5. Print_r ($ result); // output the converted result

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.