note 40495 deleted from function.explode by nlopess

From: Date: Fri, 09 Apr 2004 19:11:52 +0000
Subject: note 40495 deleted from function.explode by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-67907@lists.php.net to get a copy of this message
Note Submitter: cesschneider@yahoo.com.br ---- This function split a one dimension array into a two dimension array using regular expressions <?php /** * Split values from an array using regex * * @param array $form_values array with values from a form * @param array $match_list array with regex matches * @param array $field_list array with wished fields */ function ereg_explode($form_values, $match_list, $field_list = '') { // set a empty array $values = array(); // read all regular expressions foreach ($match_list as $name => $regex) { // read all sent fields foreach ($form_values as $field_name => $field_value) { // check if field name match with regex if (ereg($regex, $field_name)) { $field_name = ereg_replace($regex, '', $field_name); // if not defined $field_list, get all founded values if (! is_array($field_list)) { $values[$name][$field_name] = $field_value; } // get only fields defined in $field_list elseif (array_search($field_name, $field_list) !== FALSE) { $values[$name][$field_name] = $field_value; } } } } return $values; } // Example $form_values = array( name_en => 'name' ,name_es => 'nombre' ,name_pt => 'nome' ,number_en => 'number' ,number_es => 'numero' ,number_pt => 'número' ); $match_list = array( 'en' => '_en$' ,'es' => '_es$' ,'pt' => '_pt$' ); $field_list = array('name'); print '<pre>'; print_r(ereg_explode($form_values, $match_list)); print_r(ereg_explode($form_values, $match_list, $field_list)); print '</pre>'; ?>

« previous php.notes (#67907) next »