Community Documentation

Exploding a string snippet

Last updated November 16, 2008. Created by elcuco on October 17, 2008.
Edited by asak. Log in to edit this page.

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!

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.