StringHelper.php 2.57 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
			'/move$/i' => 'moves',
			'/foot$/i' => 'feet',
			'/child$/i' => 'children',
			'/human$/i' => 'humans',
			'/man$/i' => 'men',
			'/tooth$/i' => 'teeth',
			'/person$/i' => 'people',
			'/([m|l])ouse$/i' => '\1ice',
Alexander Makarov committed
39
			'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
40
			'/([^aeiouy]|qu)y$/i' => '\1ies',
Alexander Makarov committed
41
			'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
42 43 44 45 46
			'/(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
47 48
			'/s$/' => 's',
		);
49
		foreach ($rules as $rule => $replacement) {
Qiang Xue committed
50 51 52
			if (preg_match($rule, $name)) {
				return preg_replace($rule, $replacement, $name);
			}
Alexander Makarov committed
53
		}
Qiang Xue committed
54 55 56
		return $name . 's';
	}

Qiang Xue committed
57
	/**
Qiang Xue committed
58 59
	 * Converts a CamelCase name into space-separated words.
	 * For example, 'PostTag' will be converted to 'Post Tag'.
Qiang Xue committed
60 61 62 63
	 * @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
64
	public static function camel2words($name, $ucwords = true)
Qiang Xue committed
65 66 67 68 69 70
	{
		$label = trim(strtolower(str_replace(array('-', '_', '.'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
		return $ucwords ? ucwords($label) : $label;
	}

	/**
Qiang Xue committed
71 72 73
	 * 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
74
	 * @param string $name the string to be converted
Qiang Xue committed
75
	 * @param string $separator the character used to concatenate the words in the ID
Qiang Xue committed
76 77
	 * @return string the resulting ID
	 */
Qiang Xue committed
78
	public static function camel2id($name, $separator = '-')
Qiang Xue committed
79
	{
Qiang Xue committed
80 81 82 83 84
		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
85 86
	}
}