示例#1
0
/**
 * UTF8::substrReplace
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _substrReplace($str, $replacement, $offset, $length = null)
{
    if (\Phalcana\UTF8::isAscii($str)) {
        return $length === null ? substrReplace($str, $replacement, $offset) : substrReplace($str, $replacement, $offset, $length);
    }
    $length = $length === null ? \Phalcana\UTF8::strlen($str) : (int) $length;
    preg_match_all('/./us', $str, $str_array);
    preg_match_all('/./us', $replacement, $replacement_array);
    array_splice($str_array[0], $offset, $length, $replacement_array[0]);
    return implode('', $str_array[0]);
}
示例#2
0
 /**
  * Makes a singular word plural.
  *
  *     echo Inflector::plural('fish'); // "fish", uncountable
  *     echo Inflector::plural('cat');  // "cats"
  *
  * You can also provide the count to make inflection more intelligent.
  * In this case, it will only return the plural value if the count is
  * not one.
  *
  *     echo Inflector::singular('cats', 3); // "cats"
  *
  * [!!] Special inflections are defined in `config/inflector.php`.
  *
  * @param   string  $str    word to pluralize
  * @param   integer $count  count of thing
  * @return  string
  * @uses    Inflector::uncountable
  */
 public static function plural($str, $count = null)
 {
     // $count should always be a float
     $count = $count === null ? 0.0 : (double) $count;
     // Do nothing with singular
     if ($count == 1) {
         return $str;
     }
     // Remove garbage
     $str = trim($str);
     // Cache key name
     $key = 'plural_' . $str . $count;
     // Check uppercase
     $is_uppercase = ctype_upper($str);
     if (isset(self::$cache[$key])) {
         return self::$cache[$key];
     }
     if (self::uncountable($str)) {
         return self::$cache[$key] = $str;
     }
     if (empty(self::$irregular)) {
         // Cache irregular words
         self::$irregular = \Phalcana\Phalcana::$di->get('config')->load('inflector')->irregular->toArray();
     }
     if (isset(self::$irregular[$str])) {
         $str = self::$irregular[$str];
     } elseif (in_array($str, self::$irregular)) {
         // Do nothing
     } elseif (preg_match('/[sxz]$/', $str) || preg_match('/[^aeioudgkprt]h$/', $str)) {
         $str .= 'es';
     } elseif (preg_match('/[^aeiou]y$/', $str)) {
         // Change "y" to "ies"
         $str = substrReplace($str, 'ies', -1);
     } else {
         $str .= 's';
     }
     // Convert to uppercase if necessary
     if ($is_uppercase) {
         $str = strtoupper($str);
     }
     // Set the cache and return
     return self::$cache[$key] = $str;
 }