phptags
Section: Misc. Reference Manual Pages (November 2014)Updated: BSD/Linux
NAME
phptags - Converter for PHP scripts open/close tags
SYNOPSIS
phptags
[ <B>--options</B><I>,...</I> ] --options,... [ <I>files</I><B>, </B><I>path</I> ] files, path
DESCRIPTION
phptags is a simple tool to alterate <?php/?> tags in *.php scripts. It recursively reads through directories or a list of scripts and rewrites them according to specified operations:
* Remove whitespace before <? or after ?>.
* Convert <? short open tokens into <?php long tags.
* Or vice versa, long into short tags (for templates).
* Remove or add ?> close tokens.
Which is useful for regular consistency checks, or normalizing third-party code.
FILES
- *.php
-
Glob patterns can be used to specify a list of files to process. (The shell usually does the pattern matching, but unprocessed *.*
patterns are also accepted.)
- ./directory/
-
Given directories are recursively traversed and searched for *.php
files; php4, php5 and phtml extensions also accepted.
OPTIONS
Short options must be listed individually / unconcatenated, because long options may start with --double or -single hypen. There are more flag aliases than summarized here.
Whitespace modification
- -w, --white, --whitespace
-
Removes leading or trailing whitespace.
- -W, --warn
-
Just prints warnings when it detects any whitespace (or leading UTF-8 BOM).
Adding or removing close tags
- -c, --close
-
Appends closing ?> token when absent at end of scripts.
- -u, --unclosed, --open, --remove-closing
-
Removes trailing ?> close tag if present. (Also gets removed if there should be trailing whitespace.)
Rewriting short or long tags
- -l, --long
-
Converts short <? tags into long <?php versions. The inline-echo <?= short tag will be converted too, unless the --php54 flag is used.
- -s, --short
-
Converts unneccessary long <?php tags into short <? or <?= versions. It is not an overly sophisticated match method, but should only convert one-liners into short tags. Occurences of <?php echo and <? echo or print become <?=.
- -a, --all, --shortall
-
Converts all long <?php tags into short <? versions. This is intended for template scripts. The --tokenizer mode necessitates the --shortall option mostly, because it cannot differentiate between one-liners and code blocks.
Behaviour flags
- --php54
-
Does not rewrite <?= into long tags in --long mode, as those are always enabled for PHP versions 5.4 and later. (Not default, because 5.4 still isn't widespread.)
- --php7
-
Probe for <% ASP-style tokens, and super long <script language=PHP> tags. This is really just a mundane presence check, which does not involve rewriting or whitespace handling around those seldom tags.
- --rx, --regex
-
Prefer regex for rewriting short/long tags. This is the default. Can be ambiguous due to presence of tags within PHP string context or comments.
- -t, --token, --tokenizer
-
Use PHPs language tokenizer for rewriting short/long tags. (This is more reliable, but may not work when the short_open_tag php.ini setting hampers the tokenizer behaviour. It's also less judicious with linebreaks after opening tags than the regex mode, and doesn't honor <?php print variants as echo equivalent for <?= short conversion.)
Miscellaneous options
- -h, --help
-
Prints help text.
- -V, --version
-
Prints phptags version.
- -v, --verbose
-
More output for rewriting, warnings and also for --help.
- -D, --debug
-
Debugging messages.
- -q, --quiet
-
Does not print additional match infos in whitespace --warn mode, but just detected files.
- --new, --suffix
-
Does not overwrite processed *.php scripts, but saves changes into *.php.new filenames.
-b, --backup
-
Renames old files to filename.php~ prior overwriting.
Warning: This won't save multiple old versions. Just the last one gets copied.
-d, --dry
-
Dry run. Does not save modified files back.
- -r, --recursive
-
Specified directories are recursively scanned in any case. This behaviour can actually only be influenced in the phptags source, or overriden per config file.
- -c, --color
-
Colorizes the --warn output on Windows. This is enabled on BSD/Linux per default; the -c flag instead disables it there.
EXAMPLES
- phptags --close *.php
-
Adds closing tag to any missing *.php files in current directory.
- phptags --warn scripts/
-
Traverses given directory and warns of trailing or leading whitespace.
- phptags -w -s -c index*.php ./templates
-
Fixes whitespace, converts into short tags, and adds closing tag. Works on some index files and all php scripts in given templates directory.
FILES
/home/$USER/.config/php/phptags.php
An ordinary php script, that could contain operation parameter defaults.
-
<?php
return array(
'token' => 1, 'verbose' => 1, 'white' => 1,
);
Internal flag names mostly match the long CLI parameter names. See -D debug output for reference.
ENVIRONMENT
- XDG_CONFIG_HOME (on BSD/Linux), APPDATA (on Windows)
-
Configuration store directory instead of default ~/.config/
- PHPTAGS_CONFIG
-
More specific override to temporarily change the configuration file location / or thereby disable processing it.
For example PHPTAGS_CONFIG=. phptags -v will run without reading the presets.
CAVEATS
Single newlines after the closing ?> php token are not an actual problem for PHP. The parser/tokenizer eats a single <NL> or <CR> or <CRLF> up. It's only when people manage to append multiple of them, or intersparsed with spaces and tabs, that it can accrue as premature output.
ASP style <% tags and PHPs super long <script> tags are never rewritten. (Way too uncommon to warrant fringe support. The check just exists because of their removal with PHP "7", or PHP "8" depending on any prematurely printed books.)
BUGS
Matching for <?php tags is case-sensitive. That's incorrect. PHP tags are case-insensitive. (Like most other identifiers in PHP, unless you use e.g. an autoloader with systemic misdesigns.)
As mentioned before, the --regex
mode is not context-aware (for --long and --short modes),
thus can erroneously match and rewrite <? tags in PHP string context or comments:
-
print 'Here <?php and ?> will get mangled.';
print 'Here <? and ?> will get mangled.';
Which might actually be desirable in some cases. But use the --tokenizer
mode otherwise for maximum resiliency.
SEE ALSO
php(1)
recode(1)
fromdos(1)
Index
This document was created by man2html, using the manual pages.
Time: 12:03:40 GMT, November 11, 2014