Skip to content

Instantly share code, notes, and snippets.

@ozdemirburak
Created November 18, 2017 17:21
Show Gist options
  • Save ozdemirburak/45519dd9bd6728e840aa3a491ffc06a3 to your computer and use it in GitHub Desktop.
Save ozdemirburak/45519dd9bd6728e840aa3a491ffc06a3 to your computer and use it in GitHub Desktop.
Class that handles string operations lowercase, uppercase, ucfirst, and ucwords in Turkish, solves the Turkish İ problem.
<?php
/*
* Package: https://github.com/epigra/trstringhelper
*/
class TurkishString
{
/**
* @var array
*/
private static $alphabet = [
'B' => ['I', 'Ğ', 'Ü', 'Ş', 'İ', 'Ö', 'Ç'],
'k' => ['ı', 'ğ', 'ü', 'ş', 'i', 'ö', 'ç'],
'Bi' => ['I', 'i'],
'ki' => ['ı', 'İ']
];
/**
* @param $string
*
* @return string
*/
public static function toLower($string) : string
{
return mb_strtolower(str_replace(static::$alphabet['B'], static::$alphabet['k'], $string), 'utf-8');
}
/**
* @param $string
*
* @return string
*/
public static function toUpper($string) : string
{
return mb_strtoupper(str_replace(static::$alphabet['k'], static::$alphabet['B'], $string), 'utf-8');
}
/**
* @param $string
*
* @return string
*/
public static function ucFirst($string) : string
{
return mb_strtoupper(mb_substr(str_replace(static::$alphabet['k'], static::$alphabet['B'], $string), 0, 1, 'utf-8'), 'utf-8') . mb_strtolower(mb_substr(str_replace(static::$alphabet['B'], static::$alphabet['k'], $string), 1, mb_strlen($string, 'utf-8') - 1, 'utf-8'));
}
/**
* @param $string
*
* @return string
*/
public static function ucWords($string) : string
{
return mb_convert_case(ltrim(str_replace(static::$alphabet['Bi'], static::$alphabet['ki'], ' ' . $string)), MB_CASE_TITLE, 'utf-8');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment