用于分解长引用的PHP preg_split

时间:2014-04-03 15:58:25

标签: php regex preg-split

我一直在尝试创建一个正则表达式来分割各种经文引用。

这是我到目前为止所得到的:

$split_references = preg_split("/((, | or | and ))[^0-9(]/i", $reference);

这可能需要处理的文本样本是:

  

创世纪1:1-2:4a和诗篇136:1-9,23-26,创世纪7:1-5,11-18; 8:6-18; 9:8-13和诗篇46,创世纪22:1-18和诗篇16,出埃及记14:10-31; 15:20-21和出埃及记15:1b-13,17-18,以赛亚书55:1-11和   以赛亚书12:2-6,巴鲁3:9-15,3:32-4:4或箴言8:1-8,19-21; 9:4b-6和诗篇19,以西结书36:24-28和诗篇42,43,以西结书37:1-14和诗篇143,西番雅书3:14-20和诗篇98创世纪1:1-2:4a和诗篇136 :1-9,23-26,创世纪7:1-5,11-18; 8:6-18; 9:8-13和诗篇46,创世纪22:1-18和诗篇16,出埃及记14:10-31; 15:20-21和出埃及记15:1b-13,17-18,以赛亚书55:1-11和以赛亚书12:2-6,巴鲁3:9-15,3:32-4:4或箴言8:1 -8,19-21; 9:4b-6和诗篇19篇,以西结书36:24-28和诗篇42,43,以西结书37:1-14和诗篇143,西番雅书3:14-20和诗篇98

我目前得到的结果类型是:

Array(
[0] => Genesis 1:1-2:4a
[1] => salm 136:1-9, 23-26
[2] => enesis 7:1-5, 11-18; 8:6-18; 9:8-13
[3] => salm 46
[4] => enesis 22:1-18
[5] => salm 16
[6] => xodus 14:10-31; 15:20-21
[7] => xodus 15:1b-13, 17-18
[8] => saiah 55:1-11
[9] => saiah 12:2-6
[10] => aruch 3:9-15, 3:32-4:4
[11] => roverbs 8:1-8, 19-21; 9:4b-6
[12] => salm 19
[13] => zekiel 36:24-28
[14] => salm 42, 43
[15] => zekiel 37:1-14
[16] => salm 143
[17] => ephaniah 3:14-20
[18] => salm 98
)

注意第一个字母被切断了?我试图匹配[逗号字母]以防止在[逗号]

上打破数组

感谢您提供的任何指示! :)

Jason Silver

1 个答案:

答案 0 :(得分:4)

使用前瞻:

$split_references = preg_split("/((, | or | and ))(?=[^0-9(])/i", $reference);

这说"后面没有其中一个字符"没有"消费"那些人物。替代方案(相同 - 但负面预测而不是负面人物类的正向前瞻):

$split_references = preg_split("/((, | or | and ))(?!=[0-9(])/i", $reference);

上述结果:

Array
(
[0] => Genesis 1:1-2:4a
[1] => Psalm 136:1-9, 23-26
[2] => Genesis 7:1-5, 11-18; 8:6-18; 9:8-13
[3] => Psalm 46
[4] => Genesis 22:1-18
[5] => Psalm 16
[6] => Exodus 14:10-31; 15:20-21
[7] => Exodus 15:1b-13, 17-18
[8] => Isaiah 55:1-11
[9] => Isaiah 12:2-6
[10] => Baruch 3:9-15, 3:32-4:4
[11] => Proverbs 8:1-8, 19-21; 9:4b-6
[12] => Psalm 19
[13] => Ezekiel 36:24-28
[14] => Psalm 42, 43
[15] => Ezekiel 37:1-14
[16] => Psalm 143
[17] => Zephaniah 3:14-20
[18] => Psalm 98Genesis 1:1-2:4a
[19] => Psalm 136:1-9, 23-26
[20] => Genesis 7:1-5, 11-18; 8:6-18; 9:8-13
[21] => Psalm 46
[22] => Genesis 22:1-18
[23] => Psalm 16
[24] => Exodus 14:10-31; 15:20-21
[25] => Exodus 15:1b-13, 17-18
[26] => Isaiah 55:1-11
[27] => Isaiah 12:2-6
[28] => Baruch 3:9-15, 3:32-4:4
[29] => Proverbs 8:1-8, 19-21; 9:4b-6
[30] => Psalm 19
[31] => Ezekiel 36:24-28
[32] => Psalm 42, 43
[33] => Ezekiel 37:1-14
[34] => Psalm 143
[35] => Zephaniah 3:14-20
[36] => Psalm 98

)