User login

PHP

You cannot reassign an object in a function and expect the outside object to change

Stefan found a bug introduced in a contributed module when the authors took a passed-in object and, instead of only updating its properties as before, tried to change it by assigning a whole new object to the variable name.

This does not work:

Removing the current node's ID from an array of node IDs

Didn't work:

<?php
  // Remove the current node ID, if present.  This looks crazy but it's fast,
  // see http://lixlpixel.org/php-benchmarks/remove-array-items-by-value
  if ($nids) {
    $nids = explode(',', str_replace($current_nid . ',', '', join(',', $nids)));
  }
?>

Worked:

Unset certain values in an unkeyed PHP array

Can't be as complicated as these people make it out to be?
http://dev-tips.com/featured/remove-an-item-from-an-array-by-value

One of Agaric's approaches, below. This foreach allows one to unset just one validation function associated with a form (and adds another one below) without replacing or overriding any other validation callbacks that might be associated with the form (by the initial or other modules).

Get the lesser of two or more values with PHP

I love it when PHP has the logically named function. To get the lowest of two or more variables, min($var1, $var2)

Resolution

Get the directory name from a file path string in PHP

There is a built-in PHP function to get the directory portion of a file path from a string (lopping off the filename itself).

http://us3.php.net/dirname

Resolution

Get rid of extra spaces using PHP's preg_replace

Right there in the examples for the php function preg_replace was how to strip whitespace from a string:

<?php
$str = preg_replace('/\s\s+/', ' ', $str);
?>

Ereg replace looks prettier:

<?php
$string = trim(ereg_replace(' +', ' ', $string));
?>

However, Agaric has read in several places that preg_replace tends to be faster.

Resolution

Explode takes a parameter to stop exploding after a set number of splits

Explode has a limit parameter. Happy day at Agaric!

This code below was written offline when I was merely hoping that explode could be limited to splitting on its first parameter only the first time said parameter is seen:

Syndicate content