BaseStringHelper.php 4.72 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\helpers;
Qiang Xue committed
9 10

/**
11
 * BaseStringHelper provides concrete implementation for [[StringHelper]].
12
 *
13
 * Do not use BaseStringHelper. Use [[StringHelper]] instead.
Qiang Xue committed
14 15 16 17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @author Alex Makarov <sam@rmcreative.ru>
 * @since 2.0
 */
19
class BaseStringHelper
Qiang Xue committed
20
{
21 22 23
    /**
     * Returns the number of bytes in the given string.
     * This method ensures the string is treated as a byte array by using `mb_strlen()`.
24
     * @param string $string the string being measured for length
25 26 27 28 29 30
     * @return integer the number of bytes in the given string.
     */
    public static function byteLength($string)
    {
        return mb_strlen($string, '8bit');
    }
Qiang Xue committed
31

32 33 34
    /**
     * Returns the portion of string specified by the start and length parameters.
     * This method ensures the string is treated as a byte array by using `mb_substr()`.
35 36 37 38
     * @param string $string the input string. Must be one character or longer.
     * @param integer $start the starting position
     * @param integer $length the desired portion length
     * @return string the extracted part of string, or FALSE on failure or an empty string.
39 40 41 42 43 44
     * @see http://www.php.net/manual/en/function.substr.php
     */
    public static function byteSubstr($string, $start, $length)
    {
        return mb_substr($string, $start, $length, '8bit');
    }
Qiang Xue committed
45

46 47 48 49 50 51 52 53
    /**
     * Returns the trailing name component of a path.
     * This method is similar to the php function `basename()` except that it will
     * treat both \ and / as directory separators, independent of the operating system.
     * This method was mainly created to work on php namespaces. When working with real
     * file paths, php's `basename()` should work fine for you.
     * Note: this method is not aware of the actual filesystem, or path components such as "..".
     *
54 55
     * @param string $path A path string.
     * @param string $suffix If the name component ends in suffix this will also be cut off.
56 57 58 59 60 61 62 63 64 65 66 67
     * @return string the trailing name component of the given path.
     * @see http://www.php.net/manual/en/function.basename.php
     */
    public static function basename($path, $suffix = '')
    {
        if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) == $suffix) {
            $path = mb_substr($path, 0, -$len);
        }
        $path = rtrim(str_replace('\\', '/', $path), '/\\');
        if (($pos = mb_strrpos($path, '/')) !== false) {
            return mb_substr($path, $pos + 1);
        }
68

69 70 71 72 73 74 75 76
        return $path;
    }

    /**
     * Returns parent directory's path.
     * This method is similar to `dirname()` except that it will treat
     * both \ and / as directory separators, independent of the operating system.
     *
77
     * @param string $path A path string.
78 79 80 81 82 83 84 85 86 87 88 89
     * @return string the parent directory's path.
     * @see http://www.php.net/manual/en/function.basename.php
     */
    public static function dirname($path)
    {
        $pos = mb_strrpos(str_replace('\\', '/', $path), '/');
        if ($pos !== false) {
            return mb_substr($path, 0, $pos);
        } else {
            return '';
        }
    }
90 91
    
    /**
92 93
     * Truncates a string to the number of characters specified.
     *
94
     * @param string $string The string to truncate.
95 96 97
     * @param integer $length How many characters from original string to include into truncated string.
     * @param string $suffix String to append to the end of truncated string.
     * @param string $encoding The charset to use, defaults to charset currently used by application.
98 99
     * @return string the truncated string.
     */
Alex-Code committed
100
    public static function truncate($string, $length, $suffix = '...', $encoding = null)
101
    {
Alex-Code committed
102
        if (mb_strlen($string, $encoding ?: \Yii::$app->charset) > $length) {
Alex-Code committed
103
            return trim(mb_substr($string, 0, $length, $encoding ?: \Yii::$app->charset)) . $suffix;
Alex-Code committed
104 105 106
        } else {
            return $string;
        }
107 108 109
    }
    
    /**
110 111
     * Truncates a string to the number of words specified.
     *
112
     * @param string $string The string to truncate.
113 114
     * @param integer $count How many words from original string to include into truncated string.
     * @param string $suffix String to append to the end of truncated string.
115 116 117 118
     * @return string the truncated string.
     */
    public static function truncateWords($string, $count, $suffix = '...')
    {
Alex-Code committed
119
        $words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
Alex-Code committed
120 121 122 123 124
        if (count($words) / 2 > $count) {
            return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
        } else {
            return $string;
        }
125
    }
Qiang Xue committed
126
}