@@ -593,7 +593,7 @@ public static function stringToInts(string $string, &$exceptions = false): array
593593
594594 // Look for right-most alpha char. This should separate book name from numerical ref.
595595 preg_match ('/.*([a-zA-Z])/ ' , $ string , $ matches , PREG_OFFSET_CAPTURE );
596- $ spaceIndex = (int )$ matches [1 ][1 ] + 1 ; // Space index may not may not actually be a space.
596+ $ spaceIndex = (int )$ matches [1 ][1 ] + 1 ; // Space index may not actually be a space.
597597 $ book = trim (substr ($ string , 0 , $ spaceIndex ));
598598 $ ref = substr ($ string , $ spaceIndex );
599599
@@ -981,21 +981,38 @@ public static function extractFromString(string $string, bool $excludeAllBookOnl
981981 }
982982 unset($ allBookNames );
983983
984+ $ combinedMatches = [];
984985 foreach ($ regExSets as $ re ) {
985- $ b = implode ("| " , $ re ['bs ' ]);
986- $ plusOrStar = $ re ['ps ' ];
987- /** @noinspection RegExpUnnecessaryNonCapturingGroup -- They really are necessary. */
988- $ pattern = "/\b(?: $ b)\.?(?:[-\s,;&]*1?\d{1,2}:?(?:1?\d{1,2})?) $ plusOrStar\b/i " ;
989-
990- preg_match_all ($ pattern , $ string , $ matches );
991-
992- foreach ($ matches [0 ] as $ m ) {
993- $ ints = static ::stringToInts ($ m , $ exceptions );
994- foreach ($ ints as $ i ) {
995- $ results [] = new static ($ i );
996- }
997- }
998- }
986+ $ b = implode ("| " , $ re ['bs ' ]);
987+ $ plusOrStar = $ re ['ps ' ];
988+ /** @noinspection RegExpUnnecessaryNonCapturingGroup -- They really are necessary. */
989+ $ pattern = "/\b(?: $ b)\.?(?:[-\s,;&]*1?\d{1,2}:?(?:1?\d{1,2})?) $ plusOrStar\b/i " ;
990+
991+ preg_match_all ($ pattern , $ string , $ matches , PREG_OFFSET_CAPTURE );
992+
993+ $ combinedMatches = array_merge ($ combinedMatches , $ matches [0 ]);
994+ }
995+
996+ // Sort matches by position in string
997+ usort ($ combinedMatches , function ($ a , $ b ) {
998+ return $ a [1 ] - $ b [1 ];
999+ });
1000+
1001+ $ lastBook = -1 ;
1002+ $ lastEnd = -1 ;
1003+ foreach ($ combinedMatches as $ m ) {
1004+ $ ints = static ::stringToInts ($ m [0 ], $ exceptions );
1005+ foreach ($ ints as $ i ) {
1006+ $ sn = new static ($ i );
1007+ if ($ lastBook != $ sn ->book && $ lastEnd > $ m [1 ]) {
1008+ // See issue #14
1009+ continue ;
1010+ }
1011+ $ results [] = $ sn ;
1012+ $ lastBook = $ sn ->book ;
1013+ }
1014+ $ lastEnd = $ m [1 ] + strlen ($ m [0 ]);
1015+ }
9991016
10001017 return $ results ;
10011018 }
0 commit comments