I have 10 lines to extract from file text with other lines, some are empty but exist, others don’t exist but I have to insert them anyway, with regex can split text by 10 group, I use preg_match_all to match group and generate a array with matches, but if the lines not exist not matched, I want insert values group to insert in mysql table and if the value is empty set a NULL.
I just need them only values, but first verify if keys exist and add missing keys and NULL values for this.
if the value is empty set NULL, if the group not exist in text file add a values to set a NULL to insert in table mysql.
please see UPDATE QUESTION
i should use array_push, array_key_exists or in the regex assign a name by each group?
and the last, how to loop each matches? with preg_match_all generate only one array with full match, but my table mysql only 10 columns one for each group regex.
UPDATE QUESTION: I rephrased the post 11/24/21 01.00
for example full data are 10 lines
/* First Name :NameAAAAAA LINE TO EXCLUDE : this line is to exclude Last Name :LastAAAAAA LINE TO EXCLUDE : this line is to exclude Gender = (F/M) : ANOTHER LINE TO EXCLUDE : this line is to exclude TEST INFO 1 :TI1AAAAAA ANOTHER LINE TO EXCLUDE : this line is to exclude TEST INFO 2 : TEST INFO 3 :TI3AAAAAA First Name :NameBBBBBB LINE TO EXCLUDE : this line is to exclude Last Name :LastBBBBBB LINE TO EXCLUDE : this line is to exclude Gender = (F/M) : First Name :NameCCCCCC LINE TO EXCLUDE : this line is to exclude Last Name :LastCCCCCC LINE TO EXCLUDE : this line is to exclude Gender = (F/M) :M ANOTHER LINE TO EXCLUDE : this line is to exclude TEST INFO 1 : ANOTHER LINE TO EXCLUDE : this line is to exclude TEST INFO 2 :TI2CCCCCC TEST INFO 3 : */
to extract only lines (6 lines) with regex:
https://regex101.com/r/hWzvOr/1
/.*(?:First Name).*|.*(?:Last Name).*|.*(?:Gender = (F/M)).*|.*(?:TEST INFO 1).*|.*(?:TEST INFO 2).*|.*(?:TEST INFO 3).*/g
following string after extract with regex pattern: it’s possibile founds not exist lines (see TEST INFO 1-2-3 in group NameBBBBBB)
$str = ' First Name :NameAAAAAA Last Name :LastAAAAAA Gender = (F/M) : TEST INFO 1 :TI1AAAAAA TEST INFO 2 : TEST INFO 3 :TI3AAAAAA First Name :NameBBBBBB Last Name :LastBBBBBB Gender = (F/M) : First Name :NameCCCCCC Last Name :LastCCCCCC Gender = (F/M) :M TEST INFO 1 : TEST INFO 2 :TI2CCCCCC TEST INFO 3 : ';
if no exist (no match) create key by dynamic and value set to NULL, if value is empty set to NULL.
must generate, (see TEST INFO 1-2-3 in group NameBBBBBB)
Array ( [0] => Array ( [0] => First Name :NameAAAAAA [1] => Last Name :LastAAAAAA [2] => Gender = (F/M) : [3] => TEST INFO 1 :TI1AAAAAA [4] => TEST INFO 2 : [5] => TEST INFO 3 :TI3AAAAAA [6] => First Name :NameBBBBBB [7] => Last Name :LastBBBBBB [8] => Gender = (F/M) : [9] => TEST INFO 1 : [10] => TEST INFO 2 : [11] => TEST INFO 3 : [12] => First Name :NameCCCCCC [13] => Last Name :LastCCCCCC [14] => Gender = (F/M) : M [15] => TEST INFO 1 : [16] => TEST INFO 2 :TI2CCCCCC [17] => TEST INFO 3 : ) )
how to create keys if not match in regex? the loop generate one array with all data, but i insert each loop 6 lines values in one table with 6 columns into mysql?
thanks in advance.
Regards.
Italo.
Advertisement
Answer
- Build an array with all needed keys and values set to null:
$columns = [ 'APPLICATION ID', 'STATUS P=PENDING/A=ACTIVE', /*...*/ 'RUN CYCLE DESCRIPTION' ]; $keys = array_fill_keys($columns, null);
- Build a pattern to extract keys and values present in your string. To do that, it’s totally useless to put all the needed keys in your pattern, you just need to use capture groups (named here) to isolate keys from values for each match (note that the value group is optional):
$pat = '~^ h*+ (?<key> [^:n]* [^:s] ) h* : (?<value> S+ (?:h+S+)* )? ~xm';
- Use
preg_match_all
with thePREG_UNMATCHED_AS_NULL
flag: when the optional value group isn’t matched, the returned value isnull
instead of an empty string.
With thepreg_match_all
match results, build an associative array with the$matches['key']
and$matches['value']
subarrays (note that this one has the same keys than the array$keys
except that eventually some keys are missing).
Then all you have to do is to merge the$keys
array with this new array to obtain an associative array with the key/value pairs you are interested by.
if ( preg_match_all($pat, $yourstring, $matches, PREG_UNMATCHED_AS_NULL) ) { $result = array_combine($matches['key'], $matches['value']); // $result = array_intersect_key($result, $keys); // if you need to exclude some key/value pairs from the string $result = array_merge($keys, $result); }
1,2,3 Aquafresh® 3