You are here: Re: php code formating « PHP Programming Language « IT news, forums, messages
Re: php code formating

Posted by Jon Slaughter on 05/29/07 21:21

"Toby A Inkster" <usenet200703@tobyinkster.co.uk> wrote in message
news:1dgui4-8bc.ln1@ophelia.g5n.co.uk...
> Jon Slaughter wrote:
>
>> that is, I want to take a string of php code and add white space and line
>> feeds to make it readable.... my simple method of adding newlines after
>> ;'s
>> and {'s don't work well cause some strings contain substrings with code
>> in
>> it that should not be parsed.
>
> You could try something like:
>
> <?php
>
> $sourcecode = 'echo "1";if(FALSE){echo 2;echo 2; echo 2; if(TRUE){echo
> 3;}}echo 4;';
> $sourcecode = "<?php\n{$sourcecode}\n?>\n";
> $indentlevel = 0;
> $output = '';
>
> foreach(token_get_all($sourcecode) as $token)
> {
> if (is_array($token))
> list($tokentype, $spelling) = $token;
> else
> list($tokentype, $spelling) = array(NULL, $token);
>
> if ($tokentype==T_WHITESPACE && preg_match('/\s$/', $output))
> {
> continue;
> }
> elseif ($spelling=='{')
> {
> $indentlevel++;
> }
> elseif ($spelling=='}')
> {
> $indentlevel--;
> $output = preg_replace('/\t$/i', '', $output);
> }
>
> $indent = str_repeat("\t", $indentlevel);
> $output .= $spelling;
> if ($spelling==';'||$spelling=='{'||$spelling=='}')
> $output .= "\n$indent";
> }
>
> print $output;
>
> ?>
>
>

I wanted to avoid parsing it myself because there are some issues involved.

but it seems that token_get_all does all the work for you? Looks like it
will work. I'll try and see.

Thanks,
Jon

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация