Heim  >  Artikel  >  Backend-Entwicklung  >  php写的将逗号、空格、回车分隔的字符串转换为数组的函数

php写的将逗号、空格、回车分隔的字符串转换为数组的函数

WBOY
WBOYOriginal
2016-07-25 09:05:571459Durchsuche
  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. ?>
复制代码

您可能感兴趣的文章: php空格分割文本为数组的例子 php分割中文字符串为数组的简单例子 php分割中英文字符串的几种方法 php split()字符串分割函数应用举例 php分割GBK中文乱码的解决方法 php字符串分割函数explode的例子



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn