Skip to content

Commit a4a7396

Browse files
committed
Closes #14
1 parent 0779f8a commit a4a7396

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

src/ScripturNum.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/ScripturNumPublicTests.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,4 +983,21 @@ public function test_getter() {
983983
$this->expectException(Error::class);
984984
$s->doesNotExist;
985985
}
986+
987+
988+
public function test_issue14a()
989+
{
990+
$s = "1 John 1:1-5";
991+
$a = ScripturNum::extractFromString($s);
992+
993+
$this->assertEquals($s, $a->getString());
994+
}
995+
996+
public function test_issue14b()
997+
{
998+
$s = "1 John 1:1-5";
999+
$a = ScripturNum::stringToInts($s);
1000+
1001+
$this->assertCount(1, $a);
1002+
}
9861003
}

0 commit comments

Comments
 (0)