Example of PHP string segmentation function explode

Source: Internet
Author: User
    1. $this _year = 2013;

    2. $text = <<< EOT
    3. Best wishes, f,1982, Guangdong, General Staff
    4. Lie triple, m,1981, Hebei, General Staff
    5. Shoro, f,1980, South Korea, project manager
    6. EOT;

    7. $lines = explode ("\ n", $text); Separating multiple rows of data

    8. foreach ($lines as $userinfo) {
    9. $info = Explode (",", $userinfo, 3); Split only the first three data
    10. $name = $info [0];
    11. $sex = ($info [1] = = "F")? "Female": "Male";
    12. $age = $this _year-$info [2];

    13. echo "Name: $name $sex. Age: $age
      ";

    14. }

    15. /* The output results are:

    16. Name: Best wishes for the age of the girl: 31
    17. Name: Li Sanbing male Age: 32
    18. Name: Jacques Show girl Age: 33
    19. */
    20. ?>
Copy Code

The above code, first split the text by line, and then each line of string "," to split, and take the first three data processing analysis, and then collated and output.

In addition, we introduce another built-in function of PHP implode (), which is used to concatenate arrays into strings.

Corresponding to the split-string function is the implode () function, whose alias function is called join (), and the function prototypes are as follows. String implode (string $glue, array $pieces) string join (string $glue, array $pieces)

The implode () or join () function connects the elements in the array $pieces with the specified character $glue. Here is a simple example for you to study for reference. Example 2:

    1. $fruits = Array (' Apple ', ' banana ', ' pear ');
    2. $str = Implode (",", $fruits);
    3. Echo $str;
    4. ?>
Copy Code

you may be interested in the article:php space split text Array example PHP split Chinese string array simple example PHP split in English string several methods PHP split () string segmentation Function Example PHP string segmentation function explode

  • 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.