PHP

De la bonne utilisation de strtr()

Essayez un jour de taper ceci :

<?php
$lockMessage
= "This container is currently being edited by %account";
$lockMessage = strtr($lockMessage, '%account', 'Anonymous');
?>

Amusons nous avec PHP, aujourd'hui : le JSON et le console

Le petit problème du jour est le suivant : dans une application fortement AJAX, jQuery en client side, PHP en server side, j'ai besoin d'effectuer le debug de requêtes AJAX provenant du client en POST contenant du JSON, et le retour du serveur, contenant, du JSON aussi!

Pour ceci, on pourrait utiliser Firebug, que tout le monde connait bien, mais ne m'occupant pas de la partie JS, mais du code PHP serveur, j'ai pas envie d'inspecter 3000 lignes de JS pour mettre un break point au bon endroit.

Pour ceci, petit feinte, utiliser l'onglet Console de Firebug, et PHP en command line.

Tip of the day, get a views row primary key value

This is often a problem when you pragmatically manipulate views row's : finding the primary key value of the current row being explored.
Let's have an example : A node base table based view.

<?php
 
// Load the view, and stick to default display for sample purpose.
 
$view = views_get_view('my_node_view');
 
$view->set_display(NULL);
 
// Then, execute it.
 
$view->pre_execute();
 
// You will set some other options here, like limit, pager, offset.
 
$view->execute();
?>

What then?

Code snippet: human readable to machine name

This PHP code snippet compute nice machine name (i.e. with only alpha numerical characters, keeping inside hyphens) from a formated human readable name.

Useful for some automatic machine name identifiers computing from human readable arbitrary titles, I use it sometime in Drupal modules.

<?php
/**
* Helper that generates a machine name using a provided human readable name.
*
* @param string $human_name
*   Human readable name.
*
* @return string
*   Machine name cleaned-up of any special chars.
*/
function human_to_machine($human_name) {
  return
strtolower(preg_replace(array(
   
'/[^a-zA-Z0-9]+/',
   
'/-+/',
   
'/^-+/',
   
'/-+$/',
  ), array(
'-', '-', '', ''), $human_name));
}
?>

A simple (and ugly) time-saving line of code

Update on Mai, 28 : Added a new code snippet.

Sometimes, when you are trying to debug PHP code, you'll get an ugly WSOD, without any error messages in PHP error log.

When this happens, it's always really difficult to find out why. The lack of message tells you that you you didn't wrote something that bad, but bad enougth for the framework you are working with to partially catch your error without really crashing.

So, how can you resolve this?

WTF is that!? - PHP will drive me crazy; Drupal FAPI too!

What do you think this code will output:

<?php
  $foo
= '7rray';
  echo (int)
$foo;
?>

If you answered NULL, 0 or '7rray', you're absolutely wrong. The magic with PHP is whatever you use (int) or intval() you get absolutely weird results, because it'll try to parse your string as an integer whatever is in it.

System Theme 6.x-1.0 module released

System theme administration page with Slate theme

Today I released the System Theme++ module on drupal.org. This module is a simple administration theme admin page alteration. It allow site administrator to force Drupal to switch to administration theme on arbitrary pages.

Yamm development

I am currently writing a mass synchronization module for Drupal, based on eavy objects abstraction.
I will not describe it here, you can read the project page which describe pretty well how it works.

I wanted to do some notes about development context, and some other stuff.