piratox | Voici la fonction que j'utilise pour indenter mon code HTML :
Code :
- private function indent () {
- $stringIndented=NULL;
- $indexStringIndented=0;
- $indentationLevel=0;
- for ($i=0;$i<strlen($this->html);++$i) {
- $char=substr($this->html,$i,1);
- switch ($char) {
- case '<':
- if (substr($this->html,$indexStringIndented+1,1)=='/') {
- --$indentationLevel;
- while(substr($this->html,$i,1)!='>') ++$i;
- for ($j=0;$j<$indentationLevel;++$j) $stringIndented.="\t";
- $stringIndented.=substr($this->html,$indexStringIndented,$i+1-$indexStringIndented);
- $stringIndented.="\n";
- $indexStringIndented=$i+1;
- }
- else if (substr($this->html,$indexStringIndented+1,1)!='/') {
- while(substr($this->html,$i,1)!='>') ++$i;
- if (substr($this->html,$i-1,1)!='/') {
- for ($j=0;$j<$indentationLevel;++$j) $stringIndented.="\t";
- $stringIndented.=substr($this->html,$indexStringIndented,$i+1-$indexStringIndented);
- $stringIndented.="\n";
- $indexStringIndented=$i+1;
- ++$indentationLevel;
- } else {
- for ($j=0;$j<$indentationLevel;++$j) $stringIndented.="\t";
- $stringIndented.=substr($this->html,$indexStringIndented,$i+1-$indexStringIndented);
- $stringIndented.="\n";
- $indexStringIndented=$i+1;
- }
- }
- break;
- default:
- if (substr($this->html,$i+1,1)=='<') {
- for ($j=0;$j<$indentationLevel;++$j) $stringIndented.="\t";
- $stringIndented.=substr($this->html,$indexStringIndented,$i+1-$indexStringIndented);
- $stringIndented.="\n";
- $indexStringIndented=$i+1;
- }
- }
- }
- $this->html=$stringIndented;
- }
|
|