4
0

update 2023-06-30 09:04:33

This commit is contained in:
Robot 2023-06-30 09:04:33 +02:00
parent 449d0fc72a
commit 775786ed42
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
3 changed files with 51 additions and 26 deletions

View File

@ -24,6 +24,7 @@ class DailyScripture << (F,LightGreen) >> #Green {
- parse(string $reference)
- extract(string $reference) : ?string
- replace(string $reference, string $name, ...) : string
- mb_str_replace(string $search, string $replace, ...) : string
}
note right of DailyScripture::__construct
@ -88,6 +89,18 @@ note left of DailyScripture::replace
string $name
int $number
end note
note right of DailyScripture::mb_str_replace
Build in str_replace that will work with all languages
since: 2.0.1
return: string
arguments:
string $search
string $replace
string $subject
end note
@enduml
```

View File

@ -178,10 +178,12 @@ final class DailyScripture
private function extract(string $reference): ?string
{
// Use regex to match and remove chapter:verse and their variations (if they exist) from the end of the string
$bookName = preg_replace('/\s+\d+(:(\d+([,-]\d+)*)?\s*)*$/', '', $reference);
// This new regex considers Unicode word boundaries
$bookName = preg_replace('/\b\d+(:(\d+([,-]\d+)*)?\b)*$/u', '', $reference);
// If there's no match or the remaining string is empty or numeric, return null
if (is_numeric($bookName) || trim($bookName) === '')
// The is_numeric check has been adjusted to work for Unicode strings
if (mb_strlen(trim($bookName)) === 0 || preg_match('/^\d+$/u', $bookName))
{
return null;
}
@ -201,18 +203,22 @@ final class DailyScripture
*/
private function replace(string $reference, string $name, int $number): string
{
// Split the reference into words
$words = preg_split('/(\s+)/u', $reference, -1, PREG_SPLIT_DELIM_CAPTURE);
return $this->mb_str_replace($name, "$number", $reference);
}
// Find the first occurrence of the book name in the words and replace it with the book number
$index = array_search($name, $words);
if ($index !== false)
{
$words[$index] = $number;
}
// Join the words back together
return implode('', $words);
/**
* Build in str_replace that will work with all languages
*
* @param string $search The search phrase/word
* @param string $replace The replace phrase/word
* @param string $subject The string to update
*
* @return string New updated string
* @since 2.0.1
*/
private function mb_str_replace(string $search, string $replace, string $subject): string
{
return mb_ereg_replace(preg_quote($search), $replace, $subject);
}
}

View File

@ -152,10 +152,12 @@
private function extract(string $reference): ?string
{
// Use regex to match and remove chapter:verse and their variations (if they exist) from the end of the string
$bookName = preg_replace('/\s+\d+(:(\d+([,-]\d+)*)?\s*)*$/', '', $reference);
// This new regex considers Unicode word boundaries
$bookName = preg_replace('/\b\d+(:(\d+([,-]\d+)*)?\b)*$/u', '', $reference);
// If there's no match or the remaining string is empty or numeric, return null
if (is_numeric($bookName) || trim($bookName) === '')
// The is_numeric check has been adjusted to work for Unicode strings
if (mb_strlen(trim($bookName)) === 0 || preg_match('/^\d+$/u', $bookName))
{
return null;
}
@ -175,16 +177,20 @@
*/
private function replace(string $reference, string $name, int $number): string
{
// Split the reference into words
$words = preg_split('/(\s+)/u', $reference, -1, PREG_SPLIT_DELIM_CAPTURE);
return $this->mb_str_replace($name, "$number", $reference);
}
// Find the first occurrence of the book name in the words and replace it with the book number
$index = array_search($name, $words);
if ($index !== false)
{
$words[$index] = $number;
}
// Join the words back together
return implode('', $words);
/**
* Build in str_replace that will work with all languages
*
* @param string $search The search phrase/word
* @param string $replace The replace phrase/word
* @param string $subject The string to update
*
* @return string New updated string
* @since 2.0.1
*/
private function mb_str_replace(string $search, string $replace, string $subject): string
{
return mb_ereg_replace(preg_quote($search), $replace, $subject);
}