Quick Shell Tip: Find empty lines after closing PHP tags

If you’re using PHP to output XML, and you have empty lines outside of your <?php and ?> tags, you may insert empty lines before your opening XML tag - a big no-no. Apparently, that invalidates XML.

To detect this, use this Bash code in a directory of PHP files:

for file in `find -type f -name "*.php"`; do lines=$((`wc -l ${file} | cut -d' ' -f1`+1)); pos=`grep -Hn '?>' ${file} | cut -d: -f2`;  `(($lines != $pos ))` && echo $file ; done

Disclaimer: I wrote this code while in an unnatural state (who ever is?). It may break your code, impregnate your cat, cause your baby sister to fall madly in love with your microwave, or really, anything else. It even might (definitely) fail to check for newlines before you opening <?php tag! And it’ll complain if you don’t have a closing ?> tag (you’d be surprised how … inconsistent people may be). Anyway, don’t complain to me.

Comments

Post new comment

Join the conversation! Tell me what you think:
The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apachelog>, <bash>, <c>, <cfdg>, <cpp>, <cpp-qt>, <csharp>, <css>, <drupal5>, <drupal6>, <haskell>, <html4strict>, <io>, <java-generic>, <java>, <javascript>, <latex>, <lisp>, <mysql>, <ocaml>, <ocaml-breif>, <perl>, <php>, <php-breif>, <python>, <rails>, <robots>, <ruby>, <smalltalk>, <sql>, <text>, <xml>.
  • Syntax: [amazon title|cover|info asin] Example: [amazon cover B0007M123K]
  • You may insert videos with [video:URL]
  • Web page addresses and e-mail addresses turn into links automatically.
  • Adds typographic refinements.

More information about formatting options

Be sure to read my comment policy.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 16 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.