Phpformatter
phpformatter.com
PHP Formatter – A Unique Blog about PHP
PHP is known as server side scripting language and used in all known operating systems like Windows, UNIX, Linux etc. Nowadays PHP frameworks are a step ahead of the… ... The array_count_values() function returns an array which contains the keys of the original array’s value and the value is the number of occurences.
BeautifyTools
beautifytools.com › php-beautifier.php
Online PHP Beautifier - PHP Formatter - BeautifyTools.com
Online PHP Beautifier, PHP Formatter beautifies dirty, ugly or unreadable PHP code and give it proper indentation. It supports various indentation styles such as K&R style, Allman style, Whitesmiths style and GNU style.
Codeutility
alignhash.codeutility.io
Array Beautifier: Format Hash or Array Online
Please refresh in a few seconds · If you need assistance, you can contact us
Is PHP Beautifier free to use?
Yes, PHP Beautifier is free and open-source. Since it is released under the MIT License, there are no limitations on how you can use it for either personal or professional projects.
testmu.ai
testmu.ai › home › free tools › php formatter & beautifier online
PHP Formatter & Beautifier Online | TestMu AI
What is PHP Beautifier, and what does it do?
PHP Beautifier is a command-line tool that reformats PHP code to make it more readable and consistent. It adds whitespace, line breaks, and other formatting options to improve the code's readability.
testmu.ai
testmu.ai › home › free tools › php formatter & beautifier online
PHP Formatter & Beautifier Online | TestMu AI
What are the benefits of using PHP Beautifier?
Using PHP Beautifier can improve code readability, enforce coding standards, and improve overall code quality. It also helps developers to save time and reduce errors in the development process.
testmu.ai
testmu.ai › home › free tools › php formatter & beautifier online
PHP Formatter & Beautifier Online | TestMu AI
Wtools
wtools.io › php-formatter
Online PHP Formatter, PHP Beautifier - wtools.io
Formatter PHP - online web based tool who allows you make beautiful PHP code and more readable
CleanCSS
cleancss.com › php-beautify
PHP Viewer, Formatter, Editor
Enter your messy, minified, or obfuscated PHP into the field above to have it cleaned up and made pretty. The editor above also contains helpful line numbers and syntax highlighting. There are many option to tailor the beautifier to your personal formatting tastes.
JSON Formatter
jsonformatter.org › php-formatter
PHP Formatter and PHP Beautifier free and easy to Format PHP Code Online
This PHP Formatter online tool is very powerful. PHP stands for Hypertext Preprocessor and it's scripting language that is especially suited to web development. Danish-Canadian programmer Rasmus Lerdorf has developed PHP in 1994. PHP is Fast, flexible and pragmatic, and powers everything from blog to the most popular websites in the world. Use this icon to restore the last PHP code from the browser's local storage. ... Best and Secure online PHP Formatter work well in Windows, Mac, Linux, Chrome, Firefox, Safari, and Edge.
Phillihp
phillihp.com › toolz › php-array-beautifier › php-beautifier-v2
PHP Beautifier v2 | phillihp's tech blog
If you visit this blog on a regular basis, it’s likely that you know about the PHP Array and Object Beautifier. Many times while debugging PHP, I’ve been in tough spots, forced to use print_r’s and I simply could not read them. var_dump’s are good when you don’t mind sifting Source Code.
Encrypt Online
encrypt-online.com › tools › php-beautify
PHP Beautifier Online | Format and Beautify PHP Code Instantly
PHP Array Beautifier: Beautify PHP arrays and make them more readable.
TutorialsPoint
tutorialspoint.com › online_php_formatter.htm
Online PHP Formatter | Tutorialspoint
Online PHP Formatter and Beautifier - Try online PHP Code formatter and beautifier and Editor to beautify and format PHP code using jQuery Plug-in
GoOnlineTools.com
goonlinetools.com › php-beautifier
PHP Beautifier - Best PHP Formatter | GoOnlineTools
Our PHP Beautifier Tool is Simple and Free. Using this tool you can beautify PHP data in real time, it gives proper indentation to make it more readable.
Smallseotools
smallseotools.com › online-php-formatter
PHP Formatter - Beautify Your Unorganized PHP Code Online
You can combat this issue with the help of a PHP formatter that's an online tool for beautifying PHP code. This PHP formatter online tool asks the users to enter their PHP and returns the formatted version in a few seconds. Our PHP beautifier online tool doesn't involve any hard and fast rules that a user has to follow.
Top answer 1 of 14
43
Well here is my very basic and rough script:
#!/usr/bin/php
<?php
class Token {
public $type;
public $contents;
public function __construct($rawToken) {
if (is_array($rawToken)) {
$this->type = $rawToken[0];
$this->contents = $rawToken[1];
} else {
$this->type = -1;
$this->contents = $rawToken;
}
}
}
$file = $argv[1];
$code = file_get_contents($file);
$rawTokens = token_get_all($code);
$tokens = array();
foreach ($rawTokens as $rawToken) {
$tokens[] = new Token($rawToken);
}
function skipWhitespace(&$tokens, &$i) {
global $lineNo;
$i++;
$token = $tokens[$i];
while ($token->type == T_WHITESPACE) {
$lineNo += substr($token->contents, "\n");
$i++;
$token = $tokens[$i];
}
}
function nextToken(&$j) {
global $tokens, $i;
$j = $i;
do {
$j++;
$token = $tokens[$j];
} while ($token->type == T_WHITESPACE);
return $token;
}
$OPERATORS = array('=', '.', '+', '-', '*', '/', '%', '||', '&&', '+=', '-=', '*=', '/=', '.=', '%=', '==', '!=', '<=', '>=', '<', '>', '===', '!==');
$IMPORT_STATEMENTS = array(T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE);
$CONTROL_STRUCTURES = array(T_IF, T_ELSEIF, T_FOREACH, T_FOR, T_WHILE, T_SWITCH, T_ELSE);
$WHITESPACE_BEFORE = array('?', '{', '=>');
$WHITESPACE_AFTER = array(',', '?', '=>');
foreach ($OPERATORS as $op) {
$WHITESPACE_BEFORE[] = $op;
$WHITESPACE_AFTER[] = $op;
}
$matchingTernary = false;
// First pass - filter out unwanted tokens
$filteredTokens = array();
for ($i = 0, $n = count($tokens); $i < $n; $i++) {
$token = $tokens[$i];
if ($token->contents == '?') {
$matchingTernary = true;
}
if (in_array($token->type, $IMPORT_STATEMENTS) && nextToken($j)->contents == '(') {
$filteredTokens[] = $token;
if ($tokens[$i + 1]->type != T_WHITESPACE) {
$filteredTokens[] = new Token(array(T_WHITESPACE, ' '));
}
$i = $j;
do {
$i++;
$token = $tokens[$i];
if ($token->contents != ')') {
$filteredTokens[] = $token;
}
} while ($token->contents != ')');
} elseif ($token->type == T_ELSE && nextToken($j)->type == T_IF) {
$i = $j;
$filteredTokens[] = new Token(array(T_ELSEIF, 'elseif'));
} elseif ($token->contents == ':') {
if ($matchingTernary) {
$matchingTernary = false;
} elseif ($tokens[$i - 1]->type == T_WHITESPACE) {
array_pop($filteredTokens); // Remove whitespace before
}
$filteredTokens[] = $token;
} else {
$filteredTokens[] = $token;
}
}
$tokens = $filteredTokens;
function isAssocArrayVariable($offset = 0) {
global $tokens, $i;
$j = $i + $offset;
return $tokens[$j]->type == T_VARIABLE &&
$tokens[$j + 1]->contents == '[' &&
$tokens[$j + 2]->type == T_STRING &&
preg_match('/[a-z_]+/', $tokens[$j + 2]->contents) &&
$tokens[$j + 3]->contents == ']';
}
// Second pass - add whitespace
$matchingTernary = false;
$doubleQuote = false;
for ($i = 0, $n = count($tokens); $i < $n; $i++) {
$token = $tokens[$i];
if ($token->contents == '?') {
$matchingTernary = true;
}
if ($token->contents == '"' && isAssocArrayVariable(1) && $tokens[$i + 5]->contents == '"') {
/*
* Handle case where the only thing quoted is the assoc array variable.
* Eg. "$value[key]"
*/
$quote = $tokens[$i++]->contents;
$var = $tokens[$i++]->contents;
$openSquareBracket = $tokens[$i++]->contents;
$str = $tokens[$i++]->contents;
$closeSquareBracket = $tokens[$i++]->contents;
$quote = $tokens[$i]->contents;
echo $var . "['" . $str . "']";
$doubleQuote = false;
continue;
}
if ($token->contents == '"') {
$doubleQuote = !$doubleQuote;
}
if ($doubleQuote && $token->contents == '"' && isAssocArrayVariable(1)) {
// don't echo "
} elseif ($doubleQuote && isAssocArrayVariable()) {
if ($tokens[$i - 1]->contents != '"') {
echo '" . ';
}
$var = $token->contents;
$openSquareBracket = $tokens[++$i]->contents;
$str = $tokens[++$i]->contents;
$closeSquareBracket = $tokens[++$i]->contents;
echo $var . "['" . $str . "']";
if ($tokens[$i + 1]->contents != '"') {
echo ' . "';
} else {
$i++; // process "
$doubleQuote = false;
}
} elseif ($token->type == T_STRING && $tokens[$i - 1]->contents == '[' && $tokens[$i + 1]->contents == ']') {
if (preg_match('/[a-z_]+/', $token->contents)) {
echo "'" . $token->contents . "'";
} else {
echo $token->contents;
}
} elseif ($token->type == T_ENCAPSED_AND_WHITESPACE || $token->type == T_STRING) {
echo $token->contents;
} elseif ($token->contents == '-' && in_array($tokens[$i + 1]->type, array(T_LNUMBER, T_DNUMBER))) {
echo '-';
} elseif (in_array($token->type, $CONTROL_STRUCTURES)) {
echo $token->contents;
if ($tokens[$i + 1]->type != T_WHITESPACE) {
echo ' ';
}
} elseif ($token->contents == '}' && in_array($tokens[$i + 1]->type, $CONTROL_STRUCTURES)) {
echo '} ';
} elseif ($token->contents == '=' && $tokens[$i + 1]->contents == '&') {
if ($tokens[$i - 1]->type != T_WHITESPACE) {
echo ' ';
}
$i++; // match &
echo '=&';
if ($tokens[$i + 1]->type != T_WHITESPACE) {
echo ' ';
}
} elseif ($token->contents == ':' && $matchingTernary) {
$matchingTernary = false;
if ($tokens[$i - 1]->type != T_WHITESPACE) {
echo ' ';
}
echo ':';
if ($tokens[$i + 1]->type != T_WHITESPACE) {
echo ' ';
}
} elseif (in_array($token->contents, $WHITESPACE_BEFORE) && $tokens[$i - 1]->type != T_WHITESPACE &&
in_array($token->contents, $WHITESPACE_AFTER) && $tokens[$i + 1]->type != T_WHITESPACE) {
echo ' ' . $token->contents . ' ';
} elseif (in_array($token->contents, $WHITESPACE_BEFORE) && $tokens[$i - 1]->type != T_WHITESPACE) {
echo ' ' . $token->contents;
} elseif (in_array($token->contents, $WHITESPACE_AFTER) && $tokens[$i + 1]->type != T_WHITESPACE) {
echo $token->contents . ' ';
} else {
echo $token->contents;
}
}
2 of 14
15
http://en.sourceforge.jp/projects/pdt-tools/
^^^ will give you a proper CTRL+SHIFT+F Eclipse/Aptana PHP formatter like Java.
See here for installation help.
