473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

php code formating

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
6 1744
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
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

"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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jamie Fryatt | last post by:
I have a querystring that passes through the value 001 for example, the form on the receiving page need to display that number but with 1 added to it. i have dim strnum strnum =...
2
by: The Pistoleer | last post by:
I'm looking for information (links) on formating a page intended for printing. Something more refined than the browser print menu, which tends to pring extra information in the header and footer....
0
by: Gustavo Trunci | last post by:
Hi I am using VS 2003 and in one project the IDE is not formating the code anymore. It only formats when I run the project. On other project on the same machine it works fine. I compared the...
1
by: gevayl | last post by:
Hi, Is it possible to do memo text formating in ms access 2000? I mean like underlining a portion of text in a memo field or a text field? Thank you.
3
by: Tom | last post by:
Is there any way to use conditional formating to change a control for a field in a continuous form to a textbox, option group or combobox depending on the value of another field on the form? On a...
0
by: rodrigo guerra | last post by:
where i can change the code formating that visual studio does in the code.... like if i type: if ( ) { .... } visual studio changes to:
3
by: Anders K. Jacobsen [DK] | last post by:
I simple can't get visual studio 2003 to stop messing with my HTML code. I have unchecked all formating options under Tools / Options / Text editor / HTML/XML / Format I have even tried to...
3
by: Smiley | last post by:
Hi, I know how to do confitional formating in Excel by clicking the wizard. Is this possible in MS Access ? If so, please directly where to look. I found example but nothing as to how to do it....
12
tolkienarda
by: tolkienarda | last post by:
hi all I am working on a content management service and i need some help keeping formating. what i am doing is recreating my site in an admin side and all of the articles on the site will...
0
by: Fonix | last post by:
I'm trying to make table border with pyExcelerator. As i can see there is only cell formating!? If i'm wrong pls tell me what is method to make more then one cell to have same format, other then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.