Last active
November 14, 2023 06:55
-
-
Save antimech/ee6ad39da4f3272bffe0a348f87553e6 to your computer and use it in GitHub Desktop.
Находит в строке даты при помощи регулярного выражения и добавляет к ним дни недели.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php8.1 | |
<?php | |
$string = 'Давайте устроим встречу 20.05.2022 и потом ещё одну 12.06.2022'; | |
const DAYS_OF_WEEK = [ | |
0 => 'вс', | |
1 => 'пн', | |
2 => 'вт', | |
3 => 'ср', | |
4 => 'чт', | |
5 => 'пт', | |
6 => 'сб' | |
]; | |
$pattern = '/\d{2}\.\d{2}\.\d{4}/'; | |
if (preg_match_all($pattern, $string, $matches)) { | |
$dates = $matches[0]; | |
foreach ($dates as $date) { | |
$dateTime = new DateTime($date); | |
$dateDayOfWeekNumber = $dateTime->format('w'); | |
$dayOfWeekName = DAYS_OF_WEEK[$dateDayOfWeekNumber]; | |
$strToReplace = "$date ($dayOfWeekName)"; | |
$string = str_replace($date, $strToReplace, $string); | |
} | |
} | |
// Давайте устроим встречу 20.05.2022 (пт) и потом ещё одну 12.06.2022 (вс) | |
echo $string; | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment