Home >Backend Development >PHP Tutorial >A function written in PHP to convert strings separated by commas, spaces, and carriage returns into arrays

A function written in PHP to convert strings separated by commas, spaces, and carriage returns into arrays

WBOY
WBOYOriginal
2016-07-25 09:05:571536browse
  1. /**
  2. * transform ' hello, world !' to array('hello', 'world')
  3. * url: http://bbs.it-home.org
  4. * date: 2013/2/17
  5. */
  6. function strsToArray($strs) {
  7. $result = array();
  8. $array = array();
  9. $strs = str_replace(', ', ',', $strs);
  10. $strs = str_replace("n", ',', $strs);
  11. $strs = str_replace("rn", ',', $strs);
  12. $strs = str_replace(' ', ',', $strs);
  13. $array = explode(',', $strs);
  14. foreach ($array as $key => $value) {
  15. if ('' != ( $value = trim($value))) {
  16. $result[] = $value;
  17. }
  18. }
  19. return $result;
  20. }
  21. //test http://bbs.it-home.org
  22. $strs = 'Code is poetry! WTF!';
  23. var_dump(strsToArray($strs));
  24. ?>
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 Solution to php splitting GBK Chinese garbled characters Example of php string splitting function explode



Statement:
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