split php中_PHP: split - Manual

Actually, this version is better than the last I submitted.  The goal here is to be able to engage in *multiple* delimeter removal passes; for all but the last pass, set the third value to "1", and everything should go well.

function quotesplit( $splitter=',', $s, $restore_quotes=0 )

{

//First step is to split it up into the bits that are surrounded by quotes

//and the bits that aren't. Adding the delimiter to the ends simplifies

//the logic further down

$getstrings = explode('"', $splitter.$s.$splitter);

//$instring toggles so we know if we are in a quoted string or not

$delimlen = strlen($splitter);

$instring = 0;

while (list($arg, $val) = each($getstrings))

{

if ($instring==1)

{

if( $restore_quotes )

{

//Add the whole string, untouched to the previous value in the array

$result[count($result)-1] = $result[count($result)-1].'"'.$val.'"';

} else {

//Add the whole string, untouched to the array

$result[] = $val;

}

$instring = 0;

}

else

{

//Break up the string according to the delimiter character

//Each string has extraneous delimiters around it (inc the ones we added

//above), so they need to be stripped off

$temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen+1 ) );

while(list($iarg, $ival) = each($temparray))

{

$result[] = trim($ival);

}

$instring = 1;

}

}

return $result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值