note 73923 deleted from ref.array by bobby

From: Date: Fri, 16 Mar 2007 14:50:45 +0000
Subject: note 73923 deleted from ref.array by bobby
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-124518@lists.php.net to get a copy of this message
Note Submitter: alishahnovin at hotmail dot com ---- I thought I'd clean up my code a little, and add a little versatility...ie delimiter can be anything, not simply a comma. This is more for the array_to_string function that I wrote, but the string_to_strings is useful if you need to load the variables as separate strings. <?php /** * Takes a delimited string, and returns each as a separate string * Ex. "1,2,3,4,5" -> "1","2","3","4","5", where ',' is the delimiter * @param string $s - the delimited string * @param string $d - the delimiter * @return string $r - a string with quotes around each item */ function string_to_strings($s, $d) { return array_to_string(explode($d , $s)); //requires array_to_string(array) } /** * Returns a string of all the elements in a string * Ex. array("1","2","3","4","5") -> "1","2","3","4","5" * @param array $a - the array, with string/int elements * @return string $r - a string with quotes around each item */ function array_to_string($a) { if (sizeof($a)!=1) {$d=", ";} else { $d="";} //doesn't put comma after last item if (sizeof($a)!=0) { //if there are still elements in array $a=array_reverse($a); //reverse array so last item is now first, retains original order $r = '"' . array_pop($a) . '"' . $d . array_to_string(array_reverse($a));//pops first item, and recursively calls function with original array. return $r; } } $s = "1,2,3,4,5"; print string_to_strings($s, ","); //outputs "1", "2", "3", "4", "5" $a = array("hi", "hello", "salut", "gutentag", "ciao"); print array_to_string($a); //outputs "hi", "hello", "salut", "gutentag", "ciao" ?>

« previous php.notes (#124518) next »