21,887
社区成员
发帖
与我相关
我的任务
分享
$fp = fopen('a.csv', 'w');
fputcsv($fp,array('aaa','bbb','cccc'));
fclose($fp);
<?php
class Trie {
protected $dict = array();
protected $buf = '';
function set($word, $value='') {
if(is_array($word)) foreach($word as $k=>$v) $this->set($k, $v);
$p =& $this->dict;
foreach(str_split($word) as $ch) {
if(! isset($p[$ch])) $p[$ch] = array();
$p =& $p[$ch];
}
$p['val'] = $value;
return $this;
}
function parse($str) {
$this->doc = $str;
$this->len = strlen($str);
$i = 0;
while($i < $this->len) {
$t = $this->find($this->dict, $i);
if($t) {
$i = $t;
$this->buf = '';
}else $this->buf .= $this->doc{$i++};
}
}
protected function find(&$p, $i) {
if($i >= $this->len) return $i;
$t = 0;
$n = $this->doc{$i};
if( isset($p[$n]) ) $t = $this->find($p[$n], $i+1);
if($t) return $t;
if( isset($p['val']) ) {
$ar = explode(',', $p['val']);
call_user_func_array( array($this, array_shift($ar)), $ar );
return $i;
}
return $t;
}
function __call($method, $param) {
echo "****\n$this->buf 未定义方法:$method 参数:" . join(',', $param) . "<br />\n";
}
}
class App extends Trie {
public $res = array();
protected $stack = array();
protected $keyname = '';
protected $buf = '';
function __construct() {
$this->stack[] =& $this->res;
}
protected function group() {
if(! $this->keyname) return;
$cnt = count($this->stack) - 1;
$this->stack[$cnt][$this->keyname] = array();
$this->stack[] =& $this->stack[$cnt][$this->keyname];
$this->keyname = '';
}
protected function brackets($c) {
$cnt = count($this->stack) - 1;
switch($c) {
case ')':
if($this->keyname) $this->stack[$cnt][$this->keyname] = trim($this->buf);
$this->keyname = '';
array_pop($this->stack);
break;
case '[':
if($this->keyname) $this->stack[$cnt][$this->keyname] = trim($this->buf);
break;
case ']':
$this->keyname = $this->buf;
}
$this->buf = '';
}
}
$data = <<<TXT
2016-02-10T08:27:29+00:00 DEBUG (7):Array
(
[phone] => 976464
[cname] => q q
[ccountry] => US
[cstate] => Q
[ccity] => q
[caddr] => q
[czip] => q
[hname] => q
[hemail] => 1010@qq.com
[hcountry] => US
[signure] => 5695fdb1b74280e00332a36bcb5ef09a57
[card] => RW8SC2ssEHidsmc=
[expire] => H18zMSzVvN8iQ==
[cvv] => L/QNoKkruA==
)
2016-02-15T08:27:29+00:00 DEBUG (7):Array
(
[phone] => 976464
[cname] => q q
[ccountry] => US
[cstate] => Q
[ccity] => q
[caddr] => q
[czip] => q
[hname] => q
[hemail] => 1010@qq.com
[hcountry] => US
[signure] => 5695fdb1b74280e00332a36bcb5ef09a57
[card] => RW8SC2ssEHidsmc=
[expire] => H18zMSzVvN8iQ==
[cvv] => L/QNoKkruA==
)
TXT;
$result = array();
$t = '';
$tmp = explode("\n", $data);
$p = new App;
$p->set('Array', 'group')
->set(' [', 'brackets,[')
->set('] => ', 'brackets,]')
->set(')', 'brackets,)');
for($i=0; $i<count($tmp); $i=$i+17){
if($i%17==0){
$t= 'Array'.implode('',array_slice($tmp,$i+1,17)).PHP_EOL.PHP_EOL;
$p->parse($t);
$result[] = $p->res;
}
}
print_r($result);
Array
(
[0] => Array
(
[phone] => 976464
[cname] => q q
[ccountry] => US
[cstate] => Q
[ccity] => q
[caddr] => q
[czip] => q
[hname] => q
[hemail] => 1010@qq.com
[hcountry] => US
[signure] => 5695fdb1b74280e00332a36bcb5ef09a57
[card] => RW8SC2ssEHidsmc=
[expire] => H18zMSzVvN8iQ==
[cvv] => L/QNoKkruA==
)
[1] => Array
(
[phone] => 976464
[cname] => q q
[ccountry] => US
[cstate] => Q
[ccity] => q
[caddr] => q
[czip] => q
[hname] => q
[hemail] => 1010@qq.com
[hcountry] => US
[signure] => 5695fdb1b74280e00332a36bcb5ef09a57
[card] => RW8SC2ssEHidsmc=
[expire] => H18zMSzVvN8iQ==
[cvv] => L/QNoKkruA==
)
)