992Returning output of print_r in PHP

Use it to capture output and print it in one go. For example when debugging server function with an ajax call.

$message = "hello!\n";

$a = array(
  'one' => 1,
  'two' => 2
);

$message .= print_r($a, true);

print_r($output);

prints:


/*
hello!
Array
        (
            [one] => 1
            [two] => 2
)
*/

985Customising Pagination in WordPress

Getting and Customising Pagination:

function getPagination() {
  global $wp_query;
  $a = paginate_links(array(
    'current' => max(1, get_query_var('paged')),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'aria_current' => false,
    'show_all' => true,
    'end_size' => 2,
    'mid_size' => 2,
    'prev_next' => true,
    'prev_text' => '<div class="pg_arrow left"></div>',
    'next_text' => '<div class="pg_arrow right"></div>',
  ));
  if (empty($a)) return false;
  foreach($a as &$l) {
    $l = str_replace('prev page-numbers', 'pg_item', $l);
    $l = str_replace('next page-numbers', 'pg_item', $l);
    $l = str_replace('page-numbers', 'pg_item pg_num', $l);
    $l = str_replace(' aria-current=""', '', $l);
    $l = str_replace('span', 'a', $l);
    $l = str_replace('current', 'active', $l);
  }
  return $a;
}

Calling:

if spaces are needed in front

foreach(getPagination() as $p) {
  echo "   " . $p . "\n";
}

no spaces

echo implode("\n", getPagination());

868Removing Categories on save_post

function onSavePosts($post_id) { if (wp_is_post_revision($post_id)) return; // Remove Post Categories, set post categories to empty array wp_set_post_terms($post_id, array(), 'category'); } add_action('save_post', 'onSavePosts');

853PHP define(): single or double quotes?

I recently came across the following situation. A new server, a client-defined database, a database name like this: ‘my-database’. Checking if the DB exists in PhpMyAdmin, everything checks out. Putting all the details in wp-config.php should also work, right?
define('DB_NAME', 'my-database');
Hmmm. No, not quite. Error establishing a database connection Could it really be, that the dash in ‘my-database’ messes up the DB_NAME? Apparently yes.
define('DB_NAME', "my-database");
Ok, problem solved. As for why, that’s still an open question.

791Combining stderr and stdout 2>&1

If you ever find yourself in a situation of having to call a perl script from within PHP, and you want to get the return values from the perl script, you might do it like the following:
$command = "perl /my/perl/script.pl";
$results = exec($command);
// does not print error
If the perl script generates error, you won’t be able to see them, as they are written to stderr. One solution might be to append the stderr to stdout, therefore getting it into the $results variable.
$command = "perl /my/perl/script.pl 2>&1";
$results = exec($command);
// Prints: Died at /my/perl/script.pl line 25.
As this post explains, 1 means stdout, 2 means stderr. 2>1 might look ok at first sight, but the ‘1’ will be interpreted as a filename. Therefore it has to be escaped with &1, resulting in 2>&1.

682Remove empty array fields in PHP

array_filter($my_array)
array_filter WITHOUT a callback function removes ‘false’, ‘null’ and ” fields from an array.

652PHP Arrays to Javascript Objects via JSON

Moving PHP Associative Array (Dictionaries, whatever..) over for use in Javascript. In JS Associative Array are actually objects, so we might as well make it into one.
echo "var data =" . json_encode($somatic, JSON_FORCE_OBJECT) . ";

565Stripping Characters from an NSString

NSString *stripped = [unstripped stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\t "]];
Well, Cocoa, a bit of too much syntactic nutrasweet here. Take a look at PHP and wheep:
$stripped = trim($unstripped);
(Yes, I am aware that’s somewhat of an unfair comparion, but still…)

319Changing Colors in Twitter with the API

require "twitter.lib.php";

$username = "trembl";
$password = "••••••••";

$twitter = new Twitter($username, $password);

$options = array(
	"profile_background_color" => "fff",
	"profile_text_color" => "fff",
	"profile_link_color" => "fff",
	"profile_sidebar_fill_color" => "fff",
	"profile_sidebar_border_color" => "fff"
);

$update_response = $twitter->updateProfileColors($options);
print_r($update_response);
Nice, isn’t it?

265Like explode(), only componentsSeparatedByString:

PHP explode(“, ” , “One, Two, Three”); Objective-C NSArray *listItems = [@”One, Two, Three” componentsSeparatedByString:@”, “];