How to split strings in PHP _ PHP Tutorial

Source: Internet
Author: User
Describes how to split strings in PHP. Result: Array ([0] 1 [1] 2 [2] 3 [3] 4) defines and uses the str_split () function to split the string into arrays. The length syntax indicates the number of each string.Result: Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)

Definition and usage
The str_split () function separates strings into arrays.

Length indicates the number of strings.

Str_split (string, length) the default length is 1. if it is str_split ($ str, 2), the result is Array ([0] => 12 [1] => 34)

If it is $ str = '1 2 3 4 ', use the explode () function explode ('', $ str) to separate it by space.

If the content of an article is intercepted by the number of characters: Use the strlen () function and substr () string truncation function to split the string in PHP. for example:

 
 
  1. <? Php $ len = strlen ($ row ['Game _ detail']);
  2. // Obtain the length of the string;
  3. If ($ len> 102)
  4. // Judge whether a string is greater than 102 and outputs 102 strings.
  5. {Echo $ s = substr ($ row ['Game _ detail'],
    0,102); echo '...';
  6. }
  7. Else {echo $ row ["game_detail"];
  8. // Otherwise, all are output;
  9. }
  10. ?>

That is, make a judgment before the output .. Strlen (String name ).. The range of substr (string, 0,102) to be split is 0-103. These are all the methods for separating strings in PHP.


Merge result: Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4) defines and uses the str_split () function to split the string into an Array. The length syntax indicates the number of each string...

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.