Php converts a string separated by commas, spaces, and carriage return to an array.

Source: Internet
Author: User
Php converts a string separated by commas, spaces, and carriage return to an array.

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

Articles you may be interested in: examples of php space-separated text as an array php simple example of splitting Chinese strings as an array php several methods of splitting Chinese and English strings php split () application example of string segmentation function php segmentation GBK Chinese garbled solution example of 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.