note 40495 added to function.explode
| From: | cesschneider at yahoo dot com dot br | Date: | Fri, 05 Mar 2004 13:43:54 +0000 |
| Subject: | note 40495 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66299@lists.php.net to get a copy of this message | ||
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>';
?>
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40495
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40495&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40495&report=yes
Search -- http://master.php.net/manage/user-notes.php