コード例 #1
0
ファイル: dedetag.class.php プロジェクト: hensonvip/ymroad
 /**
  *  解析模板
  *
  * @access    public
  * @return    string
  */
 function ParseTemplet()
 {
     $TagStartWord = $this->TagStartWord;
     $TagEndWord = $this->TagEndWord;
     $sPos = 0;
     $ePos = 0;
     $FullTagStartWord = $TagStartWord . $this->NameSpace . ":";
     $sTagEndWord = $TagStartWord . "/" . $this->NameSpace . ":";
     $eTagEndWord = "/" . $TagEndWord;
     $tsLen = strlen($FullTagStartWord);
     $sourceLen = strlen($this->SourceString);
     if ($sourceLen <= $tsLen + 3) {
         return;
     }
     $cAtt = new DedeAttributeParse();
     $cAtt->charToLow = $this->CharToLow;
     //遍历模板字符串,请取标记及其属性信息
     for ($i = 0; $i < $sourceLen; $i++) {
         $tTagName = '';
         //如果不进行此判断,将无法识别相连的两个标记
         if ($i - 1 >= 0) {
             $ss = $i - 1;
         } else {
             $ss = 0;
         }
         $sPos = strpos($this->SourceString, $FullTagStartWord, $ss);
         $isTag = $sPos;
         if ($i == 0) {
             $headerTag = substr($this->SourceString, 0, strlen($FullTagStartWord));
             if ($headerTag == $FullTagStartWord) {
                 $isTag = TRUE;
                 $sPos = 0;
             }
         }
         if ($isTag === FALSE) {
             break;
         }
         //判断是否已经到倒数第三个字符(可能性几率极小,取消此逻辑)
         /*
         if($sPos > ($sourceLen-$tsLen-3) )
         {
             break;
         }
         */
         for ($j = $sPos + $tsLen; $j < $sPos + $tsLen + $this->TagMaxLen; $j++) {
             if ($j > $sourceLen - 1) {
                 break;
             } else {
                 if (preg_match("/[\\/ \t\r\n]/", $this->SourceString[$j]) || $this->SourceString[$j] == $this->TagEndWord) {
                     break;
                 } else {
                     $tTagName .= $this->SourceString[$j];
                 }
             }
         }
         if ($tTagName != '') {
             $i = $sPos + $tsLen;
             $endPos = -1;
             $fullTagEndWordThis = $sTagEndWord . $tTagName . $TagEndWord;
             $e1 = strpos($this->SourceString, $eTagEndWord, $i);
             $e2 = strpos($this->SourceString, $FullTagStartWord, $i);
             $e3 = strpos($this->SourceString, $fullTagEndWordThis, $i);
             //$eTagEndWord = /} $FullTagStartWord = {tag: $fullTagEndWordThis = {/tag:xxx]
             $e1 = trim($e1);
             $e2 = trim($e2);
             $e3 = trim($e3);
             $e1 = $e1 == '' ? '-1' : $e1;
             $e2 = $e2 == '' ? '-1' : $e2;
             $e3 = $e3 == '' ? '-1' : $e3;
             //not found '{/tag:'
             if ($e3 == -1) {
                 $endPos = $e1;
                 $elen = $endPos + strlen($eTagEndWord);
             } else {
                 if ($e1 == -1) {
                     $endPos = $e3;
                     $elen = $endPos + strlen($fullTagEndWordThis);
                 } else {
                     //if '/}' more near '{dede:'、'{/dede:' , end tag is '/}', else is '{/dede:'
                     if ($e1 < $e2 && $e1 < $e3) {
                         $endPos = $e1;
                         $elen = $endPos + strlen($eTagEndWord);
                     } else {
                         $endPos = $e3;
                         $elen = $endPos + strlen($fullTagEndWordThis);
                     }
                 }
             }
             //not found end tag , error
             if ($endPos == -1) {
                 echo "Tag Character postion {$sPos}, '{$tTagName}' Error!<br />\r\n";
                 break;
             }
             $i = $elen;
             $ePos = $endPos;
             //分析所找到的标记位置等信息
             $attStr = '';
             $innerText = '';
             $startInner = 0;
             for ($j = $sPos + $tsLen; $j < $ePos; $j++) {
                 if ($startInner == 0 && ($this->SourceString[$j] == $TagEndWord && $this->SourceString[$j - 1] != "\\")) {
                     $startInner = 1;
                     continue;
                 }
                 if ($startInner == 0) {
                     $attStr .= $this->SourceString[$j];
                 } else {
                     $innerText .= $this->SourceString[$j];
                 }
             }
             //echo "<xmp>$attStr</xmp>\r\n";
             $cAtt->SetSource($attStr);
             if ($cAtt->cAttributes->GetTagName() != '') {
                 $this->Count++;
                 $CDTag = new DedeTag();
                 $CDTag->TagName = $cAtt->cAttributes->GetTagName();
                 $CDTag->StartPos = $sPos;
                 $CDTag->EndPos = $i;
                 $CDTag->CAttribute = $cAtt->cAttributes;
                 $CDTag->IsReplace = FALSE;
                 $CDTag->TagID = $this->Count;
                 $CDTag->InnerText = $innerText;
                 $this->CTags[$this->Count] = $CDTag;
             }
         } else {
             $i = $sPos + $tsLen;
             break;
         }
     }
     //结束遍历模板字符串
     if ($this->IsCache) {
         $this->SaveCache();
     }
 }
コード例 #2
0
	function ParseTemplet()
	{
		$TagStartWord = $this->TagStartWord;
		$TagEndWord = $this->TagEndWord;
		$sPos = 0; $ePos = 0;
		$FullTagStartWord =  $TagStartWord.$this->NameSpace.":";
		$sTagEndWord =  $TagStartWord."/".$this->NameSpace.":";
		$eTagEndWord = "/".$TagEndWord;
		$tsLen = strlen($FullTagStartWord);
		$sourceLen=strlen($this->SourceString);
		if( $sourceLen <= ($tsLen + 3) ) return;
		$cAtt = new DedeAttributeParse();
		$cAtt->CharToLow = $this->CharToLow;
		//遍历模板字符串,请取标记及其属性信息
		for($i=0;$i<$sourceLen;$i++)
		{
			$tTagName = "";
			
			//这个设置可以保证两全紧密相连的标记可识别
			if($i - 1 > 0) $t = $i - 1;
			else $t = 0;
			
			$sPos = strpos($this->SourceString,$FullTagStartWord,$t);
			$isTag = $sPos;
			if($i==0){
				$headerTag = substr($this->SourceString,0,strlen($FullTagStartWord));
				if($headerTag==$FullTagStartWord){ $isTag=true; $sPos=0; }
			}
			if($isTag===FALSE) break;
 			if($sPos > ($sourceLen-$tsLen-3) ) break;
			
			for($j=($sPos+$tsLen);$j<($sPos+$tsLen+$this->TagMaxLen);$j++)
			{
				if($j>($sourceLen-1)) break;
				else if(ereg("[ \t\r\n]",$this->SourceString[$j])
					||$this->SourceString[$j] == $this->TagEndWord) break;
				else $tTagName .= $this->SourceString[$j];
			}
			if(strtolower($tTagName)=="comments")
			{
				$endPos = strpos($this->SourceString,$sTagEndWord ."comments",$i);
				if($endPos!==false) $i=$endPos+strlen($sTagEndWord)+8;
				continue;
			}
			$i = $sPos+$tsLen;
			$sPos = $i;
			$fullTagEndWord = $sTagEndWord.$tTagName;
			$endTagPos1 = strpos($this->SourceString,$eTagEndWord,$i);
			$endTagPos2 = strpos($this->SourceString,$fullTagEndWord,$i);
			$newStartPos = strpos($this->SourceString,$FullTagStartWord,$i);
			if($endTagPos1===FALSE) $endTagPos1=0;
			if($endTagPos2===FALSE) $endTagPos2=0;
			if($newStartPos===FALSE) $newStartPos=0;
			//判断用何种标记作为结束
			if($endTagPos1>0 && 
			  ($endTagPos1 < $newStartPos || $newStartPos==0) && 
			  ($endTagPos1 < $endTagPos2 || $endTagPos2==0 ))
			{
				$ePos = $endTagPos1;
				$i = $ePos + 2;
			}
			else if($endTagPos2>0){
				$ePos = $endTagPos2;
				$i = $ePos + strlen($fullTagEndWord)+1;
			}
			else{
				echo "Parse error the tag ".($this->GetCount()+1)." $tTagName' is incorrect ! <br/>";
			}
			//分析所找到的标记位置等信息
			$attStr = "";
			$innerText = "";
			$startInner = 0;
			for($j=$sPos;$j < $ePos;$j++)
			{
				if($startInner==0 && $this->SourceString[$j]==$TagEndWord)
				{ $startInner=1; continue; }
				if($startInner==0) $attStr .= $this->SourceString[$j];
				else $innerText .= $this->SourceString[$j];
			}
			$cAtt->SetSource($attStr);
			if($cAtt->CAttribute->GetTagName()!="")
			{
				$this->Count++;
				$CDTag = new DedeTag();
				$CDTag->TagName = $cAtt->CAttribute->GetTagName();
				$CDTag->StartPos = $sPos - $tsLen;
				$CDTag->EndPos = $i;
				$CDTag->CAttribute = $cAtt->CAttribute;
				$CDTag->IsReplace = FALSE;
				$CDTag->TagID = $this->Count;
				$CDTag->InnerText = $innerText;
				$this->CTags[$this->Count] = $CDTag;
				//定义函数或执行PHP语句
				if( $CDTag->TagName == "define"){
				  @eval($CDTag->InnerText);
			  }
			}	
		}//结束遍历模板字符串
		if($this->IsCache) $this->SaveCache();
	}