I’m trying to match the contents of a foreach loop between
two hyphens to four numbers (no [A-Z]) and discard/unset
if these don’t match. Bonus points if you can consolidate my
code below into the preg_match/replace to keep these at 7
or under innumerations.
[code=php]$data = array( ‘geo_num’ => “‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’”);
[code=php]
foreach($data AS $k=>$v) {
if($k == ‘geo_num’) {
$tmp = $v;
if(sizeof(explode(‘,’, $tmp)) > 7) {
$tmp = explode(‘,’, $tmp);
foreach($tmp AS $k => $v) {
if($v != preg_match(‘/^[0-9]{4}/’)) {
// Don’t think the above works, validation?
}
$size = sizeof($tmp);
for($i=7; $i < $size; ++$i) { unset($tmp[$i]); }
$validated[$k] = implode(‘,’, $tmp);
} else { $validated[$k] = $v; }
}