<?php
declare(strict_types=1);
echo StringInfo::getFirstTwoNumericBlocks('1234-abc-5678-def-9g0h') . "\n";
echo StringInfo::replaceLetters('1234-abc-5678-def-9g0h') . "\n";
echo StringInfo::getLettersInLowercase('1234-abc-5678-def-9g0h') . "\n";
echo StringInfo::isStringContainsSubstr('1234-abc-5678-def-9g0h') . "\n";
echo StringInfo::checkStringBeginning('1234-abc-5678-def-9g0h') . "\n";
echo StringInfo::checkStringEnding('1234-abc-5678-def-1a2b') . "\n";
class StringInfo
{
public static function getFirstTwoNumericBlocks(string $string)
{
$stringExploded = explode('-', $string);
return $stringExploded[0] . $stringExploded[2];
}
public static function replaceLetters(string $string)
{
$result = '';
$splittedString = str_split($string);
foreach ($splittedString as $symbol) {
if (ctype_alpha($symbol)) {
$result .= '*';
continue;
}
$result .= $symbol;
}
return $result;
}
public static function getLettersInLowercase(string $string)
{
$stringExploded = explode('-', $string);
return strtolower($stringExploded[1] . '/' . $stringExploded[3] . '/' .
substr($stringExploded[4], 1,1) . '/' .
substr($stringExploded[4], 3, 1));
}
/*- Вывести на экран буквы из номера документа в формате
"Letters:yyy/yyy/y/y" в верхнем регистре(реализовать с помощью
класса StringBuilder).
Не знаю, что за класс StringBuilder, но вообще будет почти как в методе выше, только:
return 'Letters:' . strtoupper($stringExploded[1] . '/' . $stringExploded[3] . '/' .
substr($stringExploded[4], 1,1) . '/' .
substr($stringExploded[4], 3, 1));
*/
public static function isStringContainsSubstr(string $string)
{
return (str_contains($string, 'abc') || str_contains($string, 'ABC'))
? 'Содержит abc/ABC'
: 'Не содержит abc/ABC';
}
public static function checkStringBeginning(string $string)
{
return (substr($string, 0, 3) == '555')
? 'Начинается с 555'
: 'Не начинается с 555';
}
public static function checkStringEnding(string $string)
{
return (substr($string, -4) == '1a2b')
? 'Заканчивается на 1a2b'
: 'Не заканчивается на 1a2b';
}
}