update 2023-06-30 09:04:33
This commit is contained in:
parent
449d0fc72a
commit
775786ed42
@ -24,6 +24,7 @@ class DailyScripture << (F,LightGreen) >> #Green {
|
|||||||
- parse(string $reference)
|
- parse(string $reference)
|
||||||
- extract(string $reference) : ?string
|
- extract(string $reference) : ?string
|
||||||
- replace(string $reference, string $name, ...) : string
|
- replace(string $reference, string $name, ...) : string
|
||||||
|
- mb_str_replace(string $search, string $replace, ...) : string
|
||||||
}
|
}
|
||||||
|
|
||||||
note right of DailyScripture::__construct
|
note right of DailyScripture::__construct
|
||||||
@ -89,6 +90,18 @@ note left of DailyScripture::replace
|
|||||||
int $number
|
int $number
|
||||||
end note
|
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
|
@enduml
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -178,10 +178,12 @@ final class DailyScripture
|
|||||||
private function extract(string $reference): ?string
|
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
|
// 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 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;
|
return null;
|
||||||
}
|
}
|
||||||
@ -201,18 +203,22 @@ final class DailyScripture
|
|||||||
*/
|
*/
|
||||||
private function replace(string $reference, string $name, int $number): string
|
private function replace(string $reference, string $name, int $number): string
|
||||||
{
|
{
|
||||||
// Split the reference into words
|
return $this->mb_str_replace($name, "$number", $reference);
|
||||||
$words = preg_split('/(\s+)/u', $reference, -1, PREG_SPLIT_DELIM_CAPTURE);
|
}
|
||||||
|
|
||||||
// Find the first occurrence of the book name in the words and replace it with the book number
|
/**
|
||||||
$index = array_search($name, $words);
|
* Build in str_replace that will work with all languages
|
||||||
if ($index !== false)
|
*
|
||||||
{
|
* @param string $search The search phrase/word
|
||||||
$words[$index] = $number;
|
* @param string $replace The replace phrase/word
|
||||||
}
|
* @param string $subject The string to update
|
||||||
|
*
|
||||||
// Join the words back together
|
* @return string New updated string
|
||||||
return implode('', $words);
|
* @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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,10 +152,12 @@
|
|||||||
private function extract(string $reference): ?string
|
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
|
// 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 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;
|
return null;
|
||||||
}
|
}
|
||||||
@ -175,16 +177,20 @@
|
|||||||
*/
|
*/
|
||||||
private function replace(string $reference, string $name, int $number): string
|
private function replace(string $reference, string $name, int $number): string
|
||||||
{
|
{
|
||||||
// Split the reference into words
|
return $this->mb_str_replace($name, "$number", $reference);
|
||||||
$words = preg_split('/(\s+)/u', $reference, -1, PREG_SPLIT_DELIM_CAPTURE);
|
}
|
||||||
|
|
||||||
// Find the first occurrence of the book name in the words and replace it with the book number
|
/**
|
||||||
$index = array_search($name, $words);
|
* Build in str_replace that will work with all languages
|
||||||
if ($index !== false)
|
*
|
||||||
{
|
* @param string $search The search phrase/word
|
||||||
$words[$index] = $number;
|
* @param string $replace The replace phrase/word
|
||||||
}
|
* @param string $subject The string to update
|
||||||
|
*
|
||||||
// Join the words back together
|
* @return string New updated string
|
||||||
return implode('', $words);
|
* @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);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user