[SOLVED] php - preg_match best way to match numbers ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
The proper way to do a preg_match or a better way to delete an array element?
I did not test your code but unset does automatically re-index the array.
Quote:
eliminate array elements with index 6, 24-35, 37 from $rows[$index]
Looks like these are the absolute index numbers but at least when I exported Thunderbird contacts my CSV file contained 37 columns but indexes are based on 0, so if you say it works...
To delete element with index 6
array_splice($array, 6, 1);
Then to delete elements from index 24-35
array_splice($array, 23, 11);
I probably got confused with the different things I was trying... With my version of Thunderbird i.e. 68.9 there isn't a column 37
Code:
Array
(
[0] => First Name
[1] => Last Name
[2] => Display Name
[3] => Nickname
[4] => Primary Email
[5] => Secondary Email
[6] => Screen Name
[7] => Work Phone
[8] => Home Phone
[9] => Fax Number
[10] => Pager Number
[11] => Mobile Number
[12] => Home Address
[13] => Home Address 2
[14] => Home City
[15] => Home State
[16] => Home ZipCode
[17] => Home Country
[18] => Work Address
[19] => Work Address 2
[20] => Work City
[21] => Work State
[22] => Work ZipCode
[23] => Work Country
[24] => Job Title
[25] => Department
[26] => Organization
[27] => Web Page 1
[28] => Web Page 2
[29] => Birth Year
[30] => Birth Month
[31] => Birth Day
[32] => Custom 1
[33] => Custom 2
[34] => Custom 3
[35] => Custom 4
[36] => Notes
)
Just saying using array_splice automatically re-indexes the array as desired.
Code:
Array
(
[0] => First Name
[1] => Last Name
[2] => Display Name
[3] => Nickname
[4] => Primary Email
[5] => Secondary Email
[6] => Work Phone
[7] => Home Phone
[8] => Fax Number
[9] => Pager Number
[10] => Mobile Number
[11] => Home Address
[12] => Home Address 2
[13] => Home City
[14] => Home State
[15] => Home ZipCode
[16] => Home Country
[17] => Work Address
[18] => Work Address 2
[19] => Work City
[20] => Work State
[21] => Work ZipCode
[22] => Work Country
[23] => Notes
)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.