您的位置:首页 > 其它

解析属性 v0.1 alpha

2007-06-05 15:03 387 查看
这个更慢,代码要简单些

$down=<<<EOD
<!--duote:title value='zdf f \tff' -->
<!--duote:list limit=10 mainTypeId=23 typeId=99 more=1 -->
软件详细页面
<!--duote:field value=sName-->

列表页
<div><!--duote:title value='高乐高 fdsafds fda' typeId= --></div>
<div><!--duote:list limit=10 mainTypeId= typeId= more=1 --></div>
EOD;

$time1 = getmicrotime();

$pt = new parseTpl;
$n=1;
for ($i=0;$i<$n ;$i++ ) {
$pt->parse($down);
}

$time1End = getmicrotime();
echo ($time1End-$time1)."<br>\n";

class parseTpl
{
function parse($data)
{
$reg = "#<!--duote:([^ \t]+)[ \t](.+)-->#isU";
preg_match_all($reg,$data,$tags);
$n=count($tags[1]); //echo $n;
for ($i=0; $i<$n ; $i++ )
{
$tagName= $tags[1][$i];
$tag = $tags[2][$i];
$attr = $this->parseTag($tag);
$attr['tagName'] = $tagName;
pr($attr);
}
//prt($tags);
}

function parseTag($tag)
{
$match = array();
$reg = "#'([^']*)'#i";
preg_match_all( $reg , $tag , $values ); //pr($values);
$tag = preg_replace( $reg , "''" , $tag );
$tagArr = preg_split( "#[\t ]+#i" , $tag ); //pr($tagArr);
foreach( $tagArr as $tagItem)
{
if(empty($tagItem))
continue;

list( $key , $value ) = explode( "=" , $tagItem ); //echo $key . "=" . $value . "<br>\n";
$attr[$key] = $value;
if($value == "''" )
{
$v = array_pop( $values[1] );
$attr[$key] = $v;
}
}
return $attr;
}

function parseDown()
{

}
}

这个比较慢,代码又复杂

$data = "list limit='10' mainTypeId='f' typeId='xd' more='1' ";
$arr = parseAtt($data);

pr($arr);

//解析属性
function parseAtt($data)
{
$startValue = false;
$startKey = true;
$attrKey = array();
$attrValue = array();
$attrSplit = array();
$n=strlen($data);
$preChar = $data[0];
for ($i=0;$i<$n ;$i++ )
{
$char = $data[$i];
if( $char=="'" && $preChar!="\\" )
{
$fg = array_pop( $attrSplit );
if( $fg=="" )
{
array_push( $attrSplit , "'");
$startValue = true;
$startKey = false;
continue;
}
else
{
array_push( $attrValue , $item );
$item = "";
$startValue = false;
continue;
}
}
elseif ( $char=="=" )
{
$startValue = true;
$startKey = false;
$preChar = $char;
array_push( $attrKey , $item );
$item = "";
continue;
}
elseif ( $char==" " || $char == "\t" )
{
if(empty($attrSplit))
{
if( $startKey===true )
{
$startValue = true;
$startKey = false;
$preChar = $char;
array_push( $attrKey , $item );
array_push( $attrValue , "" );
$item = "";
continue;
}
if( $item=="" && $startValue==false )
{
continue;
}
else
{
array_push( $attrValue , $item );
$item = "";
$startKey = true;
$startValue = false;
continue;
}
}
}
$preChar = $char;
$item .= $char;
}
if($startValue==true)
{
array_push( $attrValue , $item );
}
return array($attrKey , $attrValue);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: