PHP Snippets and Configuration

301 redirect:

header("HTTP/1.1 301 Moved Permanently");
header("Connection: close");

PHP CLI utf-8 test and encoding if not utf-8, note that this solution will break if there are no characters in the stream that need to specifically be utf-8 encoded, highly unlikely but not impossible:

#!/usr/bin/php -q
$feed = file_get_contents($_SERVER['argv'][1]);
if(!preg_match('/./u', $feed))
  echo utf8_encode($feed);
  echo $feed;

The syntax for using call_user_func_array with an object method.

$obj = new $class;
$msg = call_user_func_array(array($obj, $func), array($post));

Getting rid of WSOD (White Screen of Death), or simply the Blank Screen:
1.) Set display_errors = On in php.ini
2.) Use error_reporting(E_ALL) in the PHP code to show everything that might be interesting.
3.) You might potentially have to use restore_ error_ handler() if the framework/CMS/whatnot is using set_error_handler() to do custom error handling.

Setting a timeout of 5 secs for file_get_contents (I think the default might be 30 and that is way too much sometimes):

$context = stream_context_create(array(
    'http' => array(
        'timeout' => 5
file_get_contents("", 0, $context);

Easiest way of debugging when there is no access to output, for instance then creating various SOAP APIs:

file_put_contents("debug.txt", var_export($array, true));

If you have already installed AbiWord and the plugins you can do the following to convert a MS Word document to HTML:

$handle = popen("abiword --plugin AbiCommand 2>&1", "w");
fputs($handle, "convert DocName.doc DocName.html html");

Get the IP of a visitor:


Remove all tag attributes from a string:

preg_replace("|<(\S+)\s([^>]+)>|sim", '<${1}>', $content)