Skip to content

Instantly share code, notes, and snippets.

@tsolar
Last active August 29, 2015 14:03
Show Gist options
  • Save tsolar/d4f9212a2c4fd18ca97f to your computer and use it in GitHub Desktop.
Save tsolar/d4f9212a2c4fd18ca97f to your computer and use it in GitHub Desktop.
Whitespace fix for php
<?php
/**
* this is a fix mainly for wordpress, to prevent xml error
*
* this solution is from http://www.vichaunter.com/desarrollo-web/solucion-para-error-line-2-column-6-xml-declaration-allowed-start-document-wordpress
*
*/
function ___wejns_wp_whitespace_fix($input) {
/* valid content-type? */
$allowed = false;
/* found content-type header? */
$found = false;
/* we mangle the output if (and only if) output type is text/* */
foreach (headers_list() as $header) {
if (preg_match("/^content-type:\\s+(text\\/|application\\/((xhtml|atom|rss)\\+xml|xml))/i", $header)) {
$allowed = true;
}
if (preg_match("/^content-type:\\s+/i", $header)) {
$found = true;
}
}
/* do the actual work */
if ($allowed || !$found) {
return preg_replace("/\\A\\s*/m", "", $input);
} else {
return $input;
}
}
/* start output buffering using custom callback */
ob_start("___wejns_wp_whitespace_fix");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment