最新消息:

PHP函数第29款:字符串转化为数组,支持中英文逗号空格strsToArray

PHP扩展函数 杨红伟 5159浏览 0评论

一:函数简介

有时候,我们想将字符串转化为数组。本函数支持中文逗号,英文逗号和空格,如果想让其他符号来支持,直接修改函数就可以。很方便的!

演示地址: http://www.ijquery.cn/phpfunction/strsToArray/demo1.php

下载地址: http://www.ijquery.cn/phpfunction/strsToArray/strsToArray.zip

二:函数应用

print_r(strsToArray('hello, world!'));

返回的结果是:

Array ( [0] => hello [1] => world! ) 

三:函数代码

/**
 * 字符串转化为数组,支持中英文逗号空格
 *
 * @param     string  $strs  带有特殊符号的字符串
 * @return    int
 */
function strsToArray($strs) {
	$result = array();
	$array = array();
	$strs = str_replace(',', ',', $strs);
	$strs = str_replace(' ', ',', $strs);
	$array = explode(',', $strs);
	foreach ($array as $key => $value) {
		if ('' != ($value = trim($value))) {
			$result[] = $value;
		}
	}
	return $result;
}


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in /www/wwwroot/www.ijquery.cn/wp-content/themes/d8-3.0/comments.php on line 17
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址