=========標題========
PHP Html 轉成陣列(array)
PHP html array
PHP html to array
php html 轉 array
========HTML========
<html id ="headmenu"><head><meta id="d" /><meta /><head/></html><div id ="headmenu">3<p>sd</p><p>sd</p>bb</div>
========結果=========
array(2) { | |
[0]=> | |
array(4) { | |
["tag"]=> | |
string(6) "" | |
["id"]=> | |
string(8) "headmenu" | |
["text"]=> | |
string(0) "" | |
["next"]=> | |
array(1) { | |
[0]=> | |
array(3) { | |
["tag"]=> | |
string(6) "" | |
["text"]=> | |
string(0) "" | |
["next"]=> | |
array(2) { | |
[0]=> | |
array(2) { | |
["tag"]=> | |
string(6) "" | |
["id"]=> | |
string(1) "d" | |
} | |
[1]=> | |
array(1) { | |
["tag"]=> | |
string(6) "" | |
} | |
} | |
} | |
} | |
} | |
[1]=> | |
array(4) { | |
["tag"]=> | |
string(5) "
"
| |
["id"]=> | |
string(8) "headmenu" | |
["text"]=> | |
string(3) "3bb" | |
["next"]=> | |
array(2) { | |
[0]=> | |
array(3) { | |
["tag"]=> | |
string(3) "" | |
["text"]=> | |
string(2) "sd" | |
["next"]=> | |
array(0) { | |
} | |
} | |
[1]=> | |
array(3) { | |
["tag"]=> | |
string(3) "" | |
["text"]=> | |
string(2) "sd" | |
["next"]=> | |
array(0) { | |
} | |
} | |
} | |
} | |
} |
========程式=========
function c2a($c){ $t="";$re = array();$n=count($re); $nset=0; $ca=explode("<",$c); for($i=0;$iif($i>0){ $ca[$i] = explode(">",$ca[$i]); $ck = $this->c2i($ca[$i][0]); if(!strstr($ca[$i][0],"/")){ $nset++; if($this->taginfo($ck["tag"])){ $nset--; if($nset==0){ $n=count($re); $re[$n]=$this->c2i($ca[$i][0]); $n++; }else{ $t .= "<".$ca[$i][0].">"; } }else{ if($nset==1){ //echo "a("."<".$ca[$i][0].">".")"; $n=count($re); $re[$n]=$this->c2i($ca[$i][0]); $re[$n]["text"] = $ca[$i][1]; }else{ //echo "b("."<".$ca[$i][0].">".")"; $t .= "<".$ca[$i][0].">".$ca[$i][1]; } } }else{ $nset--; if($this->taginfo($ck["tag"])){ $nset++; if($nset==0){ $n=count($re); $re[$n]=$this->c2i($ca[$i][0]); $n++; }else{ $t .= "<".$ca[$i][0].">"; } }else{ if($nset==0){ //echo "c("."<".$ca[$i][0].">".")"; $re[$n]["next"] = $this->c2a($t); $t="";$n=count($re); }else{ //echo "d("."<".$ca[$i][0].">".")"; if(!isset($re[$n]["text"]))$re[$n]["text"]= $ca[$i][1]; else $re[$n]["text"].= $ca[$i][1]; $t .= "<".$ca[$i][0].">".$ca[$i][1]; } } } } } return $re; }
====================================
目前沒有常用到此FUNCTION
但知道一定有一些問題
請大家如果有發現問題,請告訴我,我會修正
沒有留言:
張貼留言