|
Posted by Jon Slaughter on 05/30/07 02:04
"Jon Slaughter" <Jon_Slaughter@Hotmail.com> wrote in message
news:DU47i.4505$u56.3590@newssvr22.news.prodigy.net...
>
> "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;
>>
>> ?>
>>
>>
>
> Strange, I get a debug error saying token_get_all is undefined ;/ I'm
> using php 5 for windows and its suppose to be built in ;/ Actually I'm
> using the one that comes with zend and I suppose they disabled it ;/ I
> guess theres no way to add it back in?
>
nevermind... saw that its in an exention in this case...
Navigation:
[Reply to this message]
|