note 55805 added to ref.strings

From: Date: Sat, 13 Aug 2005 23:40:18 +0000
Subject: note 55805 added to ref.strings
Groups: php.notes 
Request: Send a blank email to php-notes+get-93570@lists.php.net to get a copy of this message
A comprehensive concatenation function, that works with array and strings <?php function str_cat() { $args = func_get_args() ; // Asserts that every array given as argument is $dim-size. // Keys in arrays are stripped off. // If no array is found, $dim stays unset. foreach($args as $key => $arg) { if(is_array($arg)) { if(!isset($dim)) $dim = count($arg) ; elseif($dim != count($arg)) return FALSE ; $args[$key] = array_values($arg) ; } } // Concatenation if(isset($dim)) { $result = array() ; for($i=0;$i<$dim;$i++) { $result[$i] = '' ; foreach($args as $arg) $result[$i] .= ( is_array($arg) ? $arg[$i] : $arg ) ; } return $result ; } else { return implode($args) ; } } ?> A simple example : <?php str_cat(array(1,2,3), '-', array('foo' => 'foo', 'bar' => 'bar', 'noop' => 'noop')) ; ?> will return : Array ( [0] => 1-foo [1] => 2-bar [2] => 3-noop ) More usefull : <?php $myget = $_GET ; // retrieving previous $_GET values $myget['foo'] = 'b a r' ; // changing one value $myget = str_cat(array_keys($myget), '=', array_map('rawurlencode', array_values($myget))) ; $querystring = implode(ini_get('arg_separator.output'), $myget)) ; ?> will return a valid querystring with some values changed. Note that <?php str_cat('foo', '&', 'bar') ; ?> will return 'foo&bar', while <?php str_cat(array('foo'), '&', 'bar') ; ?> will return array(0 => foo&bar) ---- Manual Page -- http://www.php.net/manual/en/ref.strings.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55805 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55805&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55805&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55805&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55805&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55805&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55805&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55805&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93570) next »