StringHelper.php 3.14 KB
Newer Older
Alexander Makarov committed
1 2
<?php
/**
Qiang Xue committed
3
 * StringHelper class file.
Alexander Makarov committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
Alexander Makarov committed
7 8 9 10 11 12
 * @license http://www.yiiframework.com/license/
 */

namespace yii\util;

/**
Qiang Xue committed
13
 * StringHelper
Alexander Makarov committed
14
 *
Qiang Xue committed
15 16
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @author Alex Makarov <sam@rmcreative.ru>
Alexander Makarov committed
17 18
 * @since 2.0
 */
Qiang Xue committed
19
class StringHelper
Alexander Makarov committed
20 21 22
{
	/**
	 * Converts a word to its plural form.
23 24
	 * Note that this is for English only!
	 * For example, 'apple' will become 'apples', and 'child' will become 'children'.
Alexander Makarov committed
25 26 27
	 * @param string $name the word to be pluralized
	 * @return string the pluralized word
	 */
Qiang Xue committed
28
	public static function pluralize($name)
Alexander Makarov committed
29
	{
Qiang Xue committed
30
		$rules = array(
31 32 33 34 35 36 37 38
			'/(m)ove$/i' => '\1oves',
			'/(f)oot$/i' => '\1eet',
			'/(c)hild$/i' => '\1hildren',
			'/(h)uman$/i' => '\1umans',
			'/(m)an$/i' => '\1en',
			'/(s)taff$/i' => '\1taff',
			'/(t)ooth$/i' => '\1eeth',
			'/(p)erson$/i' => '\1eople',
39
			'/([m|l])ouse$/i' => '\1ice',
Alexander Makarov committed
40
			'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
41
			'/([^aeiouy]|qu)y$/i' => '\1ies',
Alexander Makarov committed
42
			'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
43 44 45 46 47
			'/(shea|lea|loa|thie)f$/i' => '\1ves',
			'/([ti])um$/i' => '\1a',
			'/(tomat|potat|ech|her|vet)o$/i' => '\1oes',
			'/(bu)s$/i' => '\1ses',
			'/(ax|test)is$/i' => '\1es',
Alexander Makarov committed
48 49
			'/s$/' => 's',
		);
50
		foreach ($rules as $rule => $replacement) {
Qiang Xue committed
51 52 53
			if (preg_match($rule, $name)) {
				return preg_replace($rule, $replacement, $name);
			}
Alexander Makarov committed
54
		}
Qiang Xue committed
55 56 57
		return $name . 's';
	}

Qiang Xue committed
58
	/**
Qiang Xue committed
59 60
	 * Converts a CamelCase name into space-separated words.
	 * For example, 'PostTag' will be converted to 'Post Tag'.
Qiang Xue committed
61 62 63 64
	 * @param string $name the string to be converted
	 * @param boolean $ucwords whether to capitalize the first letter in each word
	 * @return string the resulting words
	 */
Qiang Xue committed
65
	public static function camel2words($name, $ucwords = true)
Qiang Xue committed
66 67 68 69 70 71
	{
		$label = trim(strtolower(str_replace(array('-', '_', '.'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
		return $ucwords ? ucwords($label) : $label;
	}

	/**
Qiang Xue committed
72 73 74
	 * Converts a CamelCase name into an ID in lowercase.
	 * Words in the ID may be concatenated using the specified character (defaults to '-').
	 * For example, 'PostTag' will be converted to 'post-tag'.
Qiang Xue committed
75
	 * @param string $name the string to be converted
Qiang Xue committed
76
	 * @param string $separator the character used to concatenate the words in the ID
Qiang Xue committed
77 78
	 * @return string the resulting ID
	 */
Qiang Xue committed
79
	public static function camel2id($name, $separator = '-')
Qiang Xue committed
80
	{
Qiang Xue committed
81 82 83 84 85
		if ($separator === '_') {
			return trim(strtolower(preg_replace('/(?<![A-Z])[A-Z]/', '_\0', $name)), '_');
		} else {
			return trim(strtolower(str_replace('_', $separator, preg_replace('/(?<![A-Z])[A-Z]/', $separator . '\0', $name))), $separator);
		}
Alexander Makarov committed
86
	}
Qiang Xue committed
87 88 89 90 91 92 93 94 95 96 97 98 99

	/**
	 * Converts an ID into a CamelCase name.
	 * Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
	 * For example, 'post-tag' is converted to 'PostTag'.
	 * @param string $id the ID to be converted
	 * @param string $separator the character used to separate the words in the ID
	 * @return string the resulting CamelCase name
	 */
	public static function id2camel($id, $separator = '-')
	{
		return str_replace(' ', '', ucwords(implode(' ', explode($separator, $id))));
	}
Alexander Makarov committed
100
}