我有10行要从文件文本中提取,其中一些行是空的,但存在,其他行不存在,但我必须插入它们,使用regex可以按10组分割文本,我使用preg_match_all来匹配组并生成一个匹配数组,但如果不存在的行不匹配,我希望在mysql表中插入值组,如果值为空,则将其设置为NULL。
我只需要它们的值,但首先要验证键是否存在,并为此添加缺少的键和空值。
如果该值为空,则将其设置为NULL;如果文本文件中不存在该组,则添加一个值,将其设置为NULL,以插入表mysql。
请参阅更新问题
我应该使用array_push、array_key_exists或在regex中为每个组分配一个名称?
最后,如何循环每个匹配?使用preg_match_,all只生成一个完全匹配的数组,但我的表mysql只生成10列,每个组一个regex。
更新问题:我将帖子的措辞改为11/24/21 01.00
例如,完整数据为10行
/*
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 :
*/
要使用正则表达式仅提取行(6行):
https://regex101.com/r/hWzvOr/1
/.*(?:First Name).*|.*(?:Last Name).*|.*(?:Gender = \(F\/M\)).*|.*(?:TEST INFO 1).*|.*(?:TEST INFO 2).*|.*(?:TEST INFO 3).*/g
使用正则表达式模式提取后的以下字符串:可能会发现不存在的行(请参见组名BBBB中的测试信息1-2-3)
$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 :
';
如果不存在(不匹配),则通过动态创建密钥,并将值设置为NULL,如果值为空,则将其设置为NULL。
必须生成(参见组名BBBB中的测试信息1-2-3)
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 :
)
)
如果在正则表达式中不匹配,如何创建密钥?循环生成一个包含所有数据的数组,但我将每个循环的6行值插入一个表中,表中有6列?
提前谢谢。
当做
伊塔洛。