Example of php string splitting function explode

WBOY
Release: 2016-07-25 09:05:43
Original
1009 people have browsed it
  1. $this_year = 2013;

  2. $text = <<< EOT
  3. Zhu Wushuang, F, 1982, Guangdong, General Staff
  4. Li Sanbing, M, 1981, Hebei, ordinary employee
  5. Zhao Puxiu, F, 1980, South Korea, project manager
  6. EOT;

  7. $lines = explode("n", $text); //Separate multiple lines of data

  8. foreach ($lines as $userinfo) {
  9. $info = explode(",", $userinfo, 3); //Only split 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 result is:

  16. Name: Zhu Wushuang Female Age: 31
  17. Name: Li Sanbing Male Age: 32
  18. Name: Zhao Puxiu Female age: 33
  19. */
  20. ?>
Copy the code

The above code, first split the text by lines, and then split each line of string by ",", And take the first three data for processing and analysis, then organize and output.

In addition, I would like to introduce to you another built-in function implode() in PHP, which is used to connect arrays into strings.

Corresponding to the split string function is the implode() function. Its alias function is called join(). The function prototypes are as follows. string implode(string $glue, array $pieces) string join(string $glue, array $pieces)

implode() or join() function can connect the elements in the array $pieces with the specified character $glue. Here is a simple example for your reference. Example 2:

  1. $fruits = array('apple', 'banana', 'pear');
  2. $str = implode(", ", $fruits);
  3. echo $str;
  4. ? >
Copy code

Articles you may be interested in: Example of php space-separated text into array A simple example of splitting a Chinese string into an array in php Several ways to split Chinese and English strings in php php split() string splitting function application example Example of php string splitting function explode



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!