By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

php code formating

P: n/a
For debugging purposes of handling evaled code I first write it to a temp
file and then use include. The problem is, is that the code is not formatted
and is very hard to follow.

Is there a php library out there that will format php code so that its
readable?

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.

Thanks,
Jon
May 29 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Jon Slaughter wrote:
For debugging purposes of handling evaled code I first write it to a temp
file and then use include. The problem is, is that the code is not formatted
and is very hard to follow.

Is there a php library out there that will format php code so that its
readable?

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.

Thanks,
Jon

Amongst many others there is
http://pear.php.net/package/PHP_Beautifier


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 29 '07 #2

P: n/a
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;

?>
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 95 days, 3:39.]

Non-Intuitive Surnames
http://tobyinkster.co.uk/blog/2007/0...tive-surnames/
May 29 '07 #3

P: n/a

"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
Jon Slaughter wrote:
>For debugging purposes of handling evaled code I first write it to a temp
file and then use include. The problem is, is that the code is not
formatted and is very hard to follow.

Is there a php library out there that will format php code so that its
readable?

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.

Thanks,
Jon

Amongst many others there is
http://pear.php.net/package/PHP_Beautifier
Thanks, this looks like it might works(but I have no experience with pear
but I can probably manage).

Thanks again,
Jon
May 29 '07 #4

P: n/a

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:1d************@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
May 29 '07 #5

P: n/a

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:1d************@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?
May 30 '07 #6

P: n/a

"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:DU*****************@newssvr22.news.prodigy.ne t...
>
"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:1d************@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...

May 30 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.