Exploding a string snippet

Last modified: November 16, 2008 - 07:13

How does one 'chop up' a field you ask?

For instance, taking a field with "2.34x3.29x4.20" (let's call it field_sizes), cutting the x's out, and separating the values.

<?php
$my_field
= trim($node->sizes[0]['value'],"x");

$values = array();
$my_measures = explode("x", $my_field);
$my_delta = 0;

foreach (
$my_measures as $my_measure) {
   
$node_field[$my_delta]['value'] = trim($my_measure);
   
$my_delta++;
}
?>

and in the display format:

$display = $node_field_item['value'];

This takes field_sizes (1.12x9.32x2.12), and choppes (explodes..) it into a field with 3 values (1.12 , 9.32 , 2.12).

Enjoy!

 
 

Drupal is a registered trademark of Dries Buytaert.