php - preg_match best way to match numbers ?
by pizzipie from LinuxQuestions.org on (#55QDH)
I have rarely used reg-ex expressions and I thought I could learn a little bit of the technique now.
I am converting a thunderbird contacts address book to an 'INSERT.sql' input file for SQlite3. Part of this is deleting array elements I don't want from the header array.
Below I am using 'preg_match' to delete elements with index no's 6, 24-35 and 37.
The highlighted code I have 'settled' on (since it works ) seems to be highly 'clutzy'. Appreciate it if someone could show me the "proper" way to do this?
Code:for($i=0; $i<count($rows); $i++) { // eliminate space separating words
$str=$rows[$i];
$rows[$i]=str_replace(" ", "", $str);
}
foreach($rows[0] as $key => $value) { // eliminate array elements with index 6, 24-35, 37 from $rows[$index] ;
//echo $key."...".$value."\n";
if( preg_match('/^6/', $key) || preg_match('/^2[4-9]/', $key) || preg_match('/^3[0-5]/', $key) || preg_match('/^3[7]/', $key)) {
unset($rows[0][$key]);
} // if preg..
} // foreach


I am converting a thunderbird contacts address book to an 'INSERT.sql' input file for SQlite3. Part of this is deleting array elements I don't want from the header array.
Below I am using 'preg_match' to delete elements with index no's 6, 24-35 and 37.
The highlighted code I have 'settled' on (since it works ) seems to be highly 'clutzy'. Appreciate it if someone could show me the "proper" way to do this?
Code:for($i=0; $i<count($rows); $i++) { // eliminate space separating words
$str=$rows[$i];
$rows[$i]=str_replace(" ", "", $str);
}
foreach($rows[0] as $key => $value) { // eliminate array elements with index 6, 24-35, 37 from $rows[$index] ;
//echo $key."...".$value."\n";
if( preg_match('/^6/', $key) || preg_match('/^2[4-9]/', $key) || preg_match('/^3[0-5]/', $key) || preg_match('/^3[7]/', $key)) {
unset($rows[0][$key]);
} // if preg..
} // foreach