开发者

substr by using array as condition

I have a file contain text

ABBCDE1990-12-10JOBALPHABETabbcde1990-12-10jobalphabet


$field = array(
   "fullname" => array("length"=5,"mandat"=>True),
   "bith开发者_如何学编程day" => array("length"=>10,"mandat"=>True)
   "job" => array("length"=>3,"mandat"=>True),
   "desc" => array("length"=>8,"mandat"=>false)      
);

How can I get the array some thing like this:

$output = array(
    //ABBCDE1990-12-10JOBALPHABET
      0=>array(   
            "fullname" => "ABBCDE"
            "bithday" => 1990-12-10
            "job" =>  "JOB"
            "desc"=>  "ALPHABET"

        )
     //abbcde1990-12-10jobalphabet
       1=>array(   
            "fullname" => "abbcde"
            "bithday" => 1990-12-10
            "job" =>  "job"
            "desc"=>  "alphabet"

        )
    );

I am tryin to buld a function

function toOutput($str,$filed){
  $per_line = 27;//len of abbcde1990-12-10jobalphabet
  $pos = 0;
  while ($pos<strlen($str)){
    $pos +=  27;
    //
  }


}


$field = array(
   "fullname" => array("length"=>6,"mandat"=>True),
   "bithday" => array("length"=>10,"mandat"=>True),
   "job" => array("length"=>3,"mandat"=>True),
   "desc" => array("length"=>8,"mandat"=>false)      
);

$string = 'ABBCDE1990-12-10JOBALPHABETabbcde1990-12-10jobalphabet';

$result = array();

$countString = strlen($string)/27;
$oldPos = 0;
for($i=0;$i<$countString;$i++) {
  foreach($field as $k=>$v) {
    $result[$i][$k] = substr($string,$oldPos,$v['length']);
    $oldPos += $v['length'];
  }
}

print_r($result);

You can see it up and running here: http://codepad.org/CEQNIPCg (version 0.3)

Based on that you can create a function so you can pass to it all $string that you have

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜