You can put them into an array using the following: $string = 'Bannana Apple Orange Pear Pea'; $array = explode("\n", $string); ?> PHP:
<?php $string = 'Banana Apple Orange Pear Pea'; $array = explode("\n", $string); /* Split it up */ usort($array, create_function('$a, $b', 'return strlen($a)-strlen($b);')); /* Largest -> smallest */ $string = implode("\n", $array); /* Put it back together again */ echo $string; /* Output */ PHP:
Thanks for the replies, I've managed it using: function cmp( $a, $b ) { return strlen($a)-strlen($b) ; } $string = "pear bannana pea"; $a = explode("\n", $string); usort($a, "cmp"); foreach ($a as $key => $value) { echo "$value<br />"; } PHP:
Just a FYI usort($array, create_function('$a, $b', 'return strlen($a)-strlen($b);')); PHP: does exactly the same as all of function cmp( $a, $b ) { return strlen($a)-strlen($b) ; } usort($a, "cmp"); PHP: