PHP函数

范例来自于 PHP手册

function multiexplode ($delimiters,$string) {
	//将数组中的所有参数,都替换成数组的第一个
    $ready = str_replace($delimiters, $delimiters[0], $string);
    //再用数组的第一个参数把字符串分割成一个数组
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);

print_r($exploded);

上面的代码等同于:

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = array(",",".","|",":");
//使用正则表达式,来分割字符串
$arr = preg_split("/[,\.|:]/",$text);
print_r($arr);

‘a=>1, b=>23 , $a, c=> 45% , true,d => ab c ,6’ 转换成数组

function string2KeyedArray($string, $delimiter = ',', $kv = '=>') {
    if ($a = explode($delimiter, $string)) { // create parts
        foreach ($a as $s) { // each part
            if ($s) {
                if ($pos = strpos($s, $kv)) { // key/value delimiter
                    $ka[trim(substr($s, 0, $pos))] = trim(substr($s, $pos + strlen($kv)));
                } else { // key delimiter not found
                    $ka[] = trim($s);
                }
            }
        }
        return $ka;
    }
} // string2KeyedArray

$string = 'a=>1, b=>23   , $a, c=> 45% , true,d => ab c ,6';
print_r(string2KeyedArray($string));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值