LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2020, 07:16 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
php - preg_match best way to match numbers ?


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
 
Old 07-14-2020, 09:48 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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);

To delete element 37
array_splice($array, 25, 1);

Last edited by michaelk; 07-14-2020 at 10:00 PM.
 
Old 07-14-2020, 10:48 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks for the reply michaelk!

Quote:
The proper way to do a preg_match or a better way to delete an array element?
I'd like to see the proper way to do a preg_match

I was just wondering if this line -
Code:
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..
can be reduced in some elegant way to only one preg_match() instead of four.

Otherwise it does just what I want and the
Code:
$header=array_values($rows[0]); // headers for INSERT
re-orders the elements from 0 to 23. SEE Array's below.

Code:
Array  <======= $rows[0] - elements unset - numerical order 0->36 
(
    [0] => FirstName
    [1] => LastName
    [2] => DisplayName
    [3] => Nickname
    [4] => PrimaryEmail
    [5] => SecondaryEmail
    [7] => WorkPhone      <===========  6 not there which is good
    [8] => HomePhone
    [9] => FaxNumber
    [10] => PagerNumber
    [11] => MobileNumber
    [12] => HomeAddress
    [13] => HomeAddress2
    [14] => HomeCity
    [15] => HomeState
    [16] => HomeZipCode
    [17] => HomeCountry
    [18] => WorkAddress
    [19] => WorkAddress2
    [20] => WorkCity
    [21] => WorkState
    [22] => WorkZipCode
    [23] => WorkCountry
    [36] => Notes          <========= 24-35 not there
                           <========= 37 not there      which is good
Code:
Array  <======= $header  elements unset - numerical order 0->23 which is what I want
(
    [0] => FirstName
    [1] => LastName
    [2] => DisplayName
    [3] => Nickname
    [4] => PrimaryEmail
    [5] => SecondaryEmail
    [6] => WorkPhone
    [7] => HomePhone
    [8] => FaxNumber
    [9] => PagerNumber
    [10] => MobileNumber
    [11] => HomeAddress
    [12] => HomeAddress2
    [13] => HomeCity
    [14] => HomeState
    [15] => HomeZipCode
    [16] => HomeCountry
    [17] => WorkAddress
    [18] => WorkAddress2
    [19] => WorkCity
    [20] => WorkState
    [21] => WorkZipCode
    [22] => WorkCountry
    [23] => Notes
 
Old 07-15-2020, 04:38 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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
)

Last edited by michaelk; 07-15-2020 at 04:39 AM.
 
Old 07-15-2020, 04:48 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
  array_splice($line, 6, 1);
  array_splice($line, 23, 12);
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
)
 
Old 07-15-2020, 01:02 PM   #6
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by pizzipie View Post
Thanks for the reply michaelk!



I'd like to see the proper way to do a preg_match

I was just wondering if this line -
Code:
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..
can be reduced in some elegant way to only one preg_match() instead of four.
Off the top of my head, wouldn't this be the same thing?
Code:
preg_match('/^(6|2[4-9]|3[0-5]|3[7])/', $key)
 
2 members found this post helpful.
Old 07-15-2020, 01:24 PM   #7
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thank you SoftSprocket,

PERFECT. I knew there was a way, I just couldn't put it together properly.

R
 
Old 07-16-2020, 08:27 AM   #8
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

Also "3[0-5]|3[7]" can be simplified to "3[0-57]".

 
  


Reply

Tags
php



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[PHP] Why neither strpos nor preg_match find text with word NBK_DC_01? cesarsj Linux - Software 5 12-30-2019 02:24 PM
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
php & preg_match() Ateo Programming 1 01-19-2009 12:04 PM
php preg_match help.. Ateo Programming 3 07-18-2006 03:57 PM
PHP: call to undefined function preg_match() (I think to do with PCRE) BuckRogers01 Linux - Software 1 10-17-2005 01:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration