Skip to content

Instantly share code, notes, and snippets.

@szepeviktor
Last active November 4, 2023 22:58
Show Gist options
  • Save szepeviktor/010211df28bce0c957bd3668a7a35b59 to your computer and use it in GitHub Desktop.
Save szepeviktor/010211df28bce0c957bd3668a7a35b59 to your computer and use it in GitHub Desktop.
Határozott névelő hozzáadása egy szóhoz PHP-ban
<?php
namespace Grammar\Hungarian;
class DefiniteArticle
{
const CONSONANTS = [
'0',
'2',
'3',
'4',
'6',
'7',
'8',
'9',
'b',
'c',
'd',
'f',
'g',
'h',
'j',
'k',
'l',
'm',
'n',
'p',
'q',
'r',
's',
't',
'v',
'w',
'x',
'y',
'z',
];
const ARTICLE_FOR_CONSONANTS = 'a';
const ARTICLE_FOR_VOWELS = 'az';
public static function artikulus(string $word): string
{
$firstCharacter = mb_convert_case(mb_substr($word, 0, 1), MB_CASE_LOWER);
$article = in_array($firstCharacter, self::CONSONANTS)
? self::ARTICLE_FOR_CONSONANTS
: self::ARTICLE_FOR_VOWELS;
// 10-19
if (preg_match('/^1[0-9]\b/', $word) === 1) {
$article = self::ARTICLE_FOR_CONSONANTS;
}
return sprintf('%s %s', $article, $word);
}
public static function artikulusSentence(string $word): string
{
return ucfirst(self::artikulus($word));
}
}
@szepeviktor
Copy link
Author

szepeviktor commented Nov 3, 2023

echo DefiniteArticle::artikulus('Énekes'), "\n";
echo DefiniteArticle::artikulusSentence('cipő'), "\n";
echo DefiniteArticle::artikulus('11. nap'), "\n";
az Énekes
A cipő
a 11. nap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment