/** * Plural * * Takes a singular word and makes it plural * * @param string $str Input string * @return string */ function plural($str) { $result = strval($str); if (!is_countable($result)) { return $result; } $plural_rules = array('/^(ox)$/' => '\\1\\2en', '/([m|l])ouse$/' => '\\1ice', '/(matr|vert|ind)ix|ex$/' => '\\1ices', '/(x|ch|ss|sh)$/' => '\\1es', '/([^aeiouy]|qu)y$/' => '\\1ies', '/(hive)$/' => '\\1s', '/(?:([^f])fe|([lr])f)$/' => '\\1\\2ves', '/sis$/' => 'ses', '/([ti])um$/' => '\\1a', '/(p)erson$/' => '\\1eople', '/(m)an$/' => '\\1en', '/(c)hild$/' => '\\1hildren', '/(buffal|tomat)o$/' => '\\1\\2oes', '/(bu|campu)s$/' => '\\1\\2ses', '/(alias|status|virus)$/' => '\\1es', '/(octop)us$/' => '\\1i', '/(ax|cris|test)is$/' => '\\1es', '/s$/' => 's', '/$/' => 's'); foreach ($plural_rules as $rule => $replacement) { if (preg_match($rule, $result)) { $result = preg_replace($rule, $replacement, $result); break; } } return $result; }
public function inflector_helper() { $this->load->helper('inflector'); echo singular('dogs'); echo "<br>"; echo plural('dog'); echo "<br>"; echo camelize('my_dog_spot'); // Prints 'myDogSpot' echo "<br>"; echo underscore('my dog spot'); // Prints 'my_dog_spot' echo "<br>"; echo humanize('my_dog_spot'); // Prints 'My Dog Spot' echo "<br>"; echo humanize('my-dog-spot', '-'); // Prints 'My Dog Spot' echo "<br>"; echo is_countable('equipment'); // Returns FALSE }
/** * Test if a value is like an array (IS an array, * or implements \Traversable, \Countable and \ArrayAccess interfaces). * * @param mixed $var the value * @return bool true if the value is like an array */ function is_like_array($var) : bool { return is_array($var) || is_traversable($var) && is_countable($var) && is_array_access($var); }