|  | Posted by Juliette on 06/21/06 18:17 
Andy Jeffries wrote:> On Wed, 21 Jun 2006 08:09:39 +0000, Andy Jeffries wrote:
 >> And a bug that actually gives the incorrect output.  The first part of the
 >> string from your function is:
 >>
 >> Carson; David, Milne; Rebecca,
 >>
 >> And it should be:
 >>
 >> Carson, David; Milne, Rebecca;
 >
 > And it also has the same deficiency as Rik's function version in that it
 > adds an extra delimiter on the end.
 >
 > Cheers,
 >
 >
 > Andy
 >
 
 
 Andy,
 
 Oops.. I obviously posted too quickly (and yes, I didn't test the code
 before posting, just did it off-hand).
 
 Interesting to see the ensuing discussion this has brought. I normally
 use preg myself, but just couldn't think of how to do this one. In that
 respect I still have a nice challenge (regex) I'm working on myself
 which I can't seem to get right.
 
 Just in case someone is interested, you can find the corrected code from
 my function below. I've also gotten rid of some of the 'fluff' which I
 only put in to make the code easier to understand.
 About the extra delimiter: that was not explicite in the requirements
 given by the OP, so that's why I didn't take that into account. The
 below function does however deal with that correctly, i.e. the way you
 suggested it ought to work.
 
 $array = explode ( ', ', $string );
 $arraycount = ( count($array)-1 );
 $string = '';
 
 for( $i=0; $i<=$arraycount; $i++ ) {
 $string .= ( $i === $arraycount ) ? ( $array[$i] ) : ( (1&$i) ? (
 $array[$i] . '; ' ) : ( $array[$i] . ', ' ) );
 }
 unset( $array, $arraycount );
 
 The regex will still win, but I would guess this is slightly faster than
 my earlier function.
 
 Grz, Juliette
  Navigation: [Reply to this message] |