PluginProbe ʕ •ᴥ•ʔ
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings / 1.0.255
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings v1.0.255
1.0.273 1.0.272 1.0.271 1.0.271.1 1.0.270 1.0.269 trunk 1.0.216 1.0.217 1.0.218 1.0.219 1.0.220 1.0.221 1.0.222 1.0.223 1.0.224 1.0.225 1.0.226 1.0.227 1.0.227.1 1.0.228 1.0.229 1.0.230 1.0.231 1.0.232 1.0.233 1.0.234 1.0.234.1 1.0.235 1.0.236 1.0.237 1.0.238 1.0.239 1.0.240 1.0.241 1.0.242 1.0.243 1.0.244 1.0.245 1.0.246 1.0.247 1.0.248 1.0.249 1.0.250 1.0.251 1.0.251.1 1.0.252 1.0.252.1 1.0.253 1.0.254 1.0.255 1.0.256 1.0.257 1.0.258 1.0.259 1.0.259.1 1.0.260 1.0.261 1.0.262 1.0.263 1.0.264 1.0.264.1 1.0.265 1.0.266 1.0.266.1 1.0.267 1.0.268
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src / helpers / class-str.php
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src / helpers Last commit date
class-arr.php 2 years ago class-attachment.php 2 years ago class-conditional.php 2 years ago class-db.php 2 years ago class-html.php 2 years ago class-param.php 2 years ago class-str.php 2 years ago class-url.php 2 years ago class-wordpress.php 2 years ago index.php 2 years ago
class-str.php
243 lines
1 <?php
2 /**
3 * The String helpers.
4 *
5 * @since 1.0.0
6 * @package MyThemeShop
7 * @subpackage MyThemeShop\Helpers
8 * @author MyThemeShop <admin@mythemeshop.com>
9 */
10
11 namespace MyThemeShop\Helpers;
12
13 /**
14 * Str class.
15 */
16 class Str {
17
18 /**
19 * Validates whether the passed variable is a empty string.
20 *
21 * @param mixed $variable The variable to validate.
22 *
23 * @return bool Whether or not the passed value is a non-empty string.
24 */
25 public static function is_empty( $variable ) {
26 return empty( $variable ) || ! is_string( $variable );
27 }
28
29 /**
30 * Validates whether the passed variable is a non-empty string.
31 *
32 * @param mixed $variable The variable to validate.
33 *
34 * @return bool Whether or not the passed value is a non-empty string.
35 */
36 public static function is_non_empty( $variable ) {
37 return is_string( $variable ) && '' !== $variable;
38 }
39
40 /**
41 * Check if the string contains the given value.
42 *
43 * @param string $needle The sub-string to search for.
44 * @param string $haystack The string to search.
45 *
46 * @return bool
47 */
48 public static function contains( $needle, $haystack ) {
49 return self::is_non_empty( $needle ) ? strpos( $haystack, $needle ) !== false : false;
50 }
51
52 /**
53 * Check if the string begins with the given value.
54 *
55 * @param string $needle The sub-string to search for.
56 * @param string $haystack The string to search.
57 *
58 * @return bool
59 */
60 public static function starts_with( $needle, $haystack ) {
61 return '' === $needle || substr( $haystack, 0, strlen( $needle ) ) === (string) $needle;
62 }
63
64 /**
65 * Check if the string end with the given value.
66 *
67 * @param string $needle The sub-string to search for.
68 * @param string $haystack The string to search.
69 *
70 * @return bool
71 */
72 public static function ends_with( $needle, $haystack ) {
73 return '' === $needle || substr( $haystack, -strlen( $needle ) ) === (string) $needle;
74 }
75
76 /**
77 * Check the string for desired comparison.
78 *
79 * @param string $needle The sub-string to search for.
80 * @param string $haystack The string to search.
81 * @param string $comparison The type of comparison.
82 *
83 * @return bool
84 */
85 public static function comparison( $needle, $haystack, $comparison = '' ) {
86
87 $hash = array(
88 'regex' => 'preg_match',
89 'end' => array( __CLASS__, 'ends_with' ),
90 'start' => array( __CLASS__, 'starts_with' ),
91 'contains' => array( __CLASS__, 'contains' ),
92 );
93
94 if ( $comparison && isset( $hash[ $comparison ] ) ) {
95 return call_user_func( $hash[ $comparison ], $needle, $haystack );
96 }
97
98 // Exact.
99 return $needle === $haystack;
100 }
101
102 /**
103 * Convert string to array with defined seprator.
104 *
105 * @param string $str String to convert.
106 * @param string $sep Seprator.
107 *
108 * @return bool|array
109 */
110 public static function to_arr( $str, $sep = ',' ) {
111 $parts = explode( $sep, trim( $str ) );
112
113 return empty( $parts ) ? false : $parts;
114 }
115
116 /**
117 * Convert string to array, weed out empty elements and whitespaces.
118 *
119 * @param string $str User-defined list.
120 * @param string $sep_pattern Separator pattern for regex.
121 *
122 * @return array
123 */
124 public static function to_arr_no_empty( $str, $sep_pattern = '\r\n|[\r\n]' ) {
125 $array = empty( $str ) ? array() : preg_split( '/' . $sep_pattern . '/', $str, -1, PREG_SPLIT_NO_EMPTY );
126 $array = array_filter( array_map( 'trim', $array ) );
127
128 return $array;
129 }
130
131 /**
132 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
133 *
134 * @param string $size The size.
135 *
136 * @return int
137 */
138 public static function let_to_num( $size ) {
139 $char = substr( $size, -1 );
140 $ret = substr( $size, 0, -1 );
141
142 // @codingStandardsIgnoreStart
143 switch ( strtoupper( $char ) ) {
144 case 'P':
145 $ret *= 1024;
146 case 'T':
147 $ret *= 1024;
148 case 'G':
149 $ret *= 1024;
150 case 'M':
151 $ret *= 1024;
152 case 'K':
153 $ret *= 1024;
154 }
155 // @codingStandardsIgnoreEnd
156
157 return $ret;
158 }
159
160 /**
161 * Convert a number to K, M, B, etc.
162 *
163 * @param int|double $number Number which to convert to pretty string.
164 * @param int $precision Decimal places in the human-readable format.
165 *
166 * @return string
167 */
168 public static function human_number( $number, $precision = 1 ) {
169
170 if ( ! is_numeric( $number ) ) {
171 return 0;
172 }
173
174 $negative = '';
175 if ( abs( $number ) != $number ) {
176 $negative = '-';
177 $number = abs( $number );
178 }
179
180 if ( $number < 1000 ) {
181 return $negative ? -1 * $number : $number;
182 }
183
184 $unit = intval( log( $number, 1000 ) );
185 $units = array( '', 'K', 'M', 'B', 'T', 'Q' );
186
187 if ( array_key_exists( $unit, $units ) ) {
188 return sprintf( '%s%s%s', $negative, rtrim( number_format( $number / pow( 1000, $unit ), $precision ), '.0' ), $units[ $unit ] );
189 }
190
191 return $number;
192 }
193
194 /**
195 * Truncate text for given length.
196 *
197 * @param {string} $str Text to truncate.
198 * @param {number} $length Length to truncate for.
199 * @param {string} $append Append to the end if string is truncated.
200 *
201 * @return {string} Truncated text.
202 */
203 public static function truncate( $str, $length = 110, $append = '' ) {
204 $str = wp_strip_all_tags( $str, true );
205 $strlen = mb_strlen( $str );
206 $excerpt = mb_substr( $str, 0, $length );
207
208 // Remove part of an entity at the end.
209 $excerpt = preg_replace( '/&[^;\s]{0,6}$/', '', $excerpt );
210 if ( $str !== $excerpt ) {
211 $strrpos = function_exists( 'mb_strrpos' ) ? 'mb_strrpos' : 'strrpos';
212 $excerpt = mb_substr( $str, 0, $strrpos( trim( $excerpt ), ' ' ) );
213 }
214
215 if ( $strlen > $length ) {
216 $excerpt .= $append;
217 }
218
219 return $excerpt;
220 }
221
222 /**
223 * Multibyte ucwords.
224 *
225 * @param string $string String to convert.
226 */
227 public static function mb_ucwords( $string ) {
228 if ( ! function_exists( 'mb_convert_case' ) || ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $string ) !== 'UTF-8' ) {
229 return ucwords( $string );
230 }
231
232 $words = preg_split( '/([\s]+)/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
233 $ucwords = '';
234 foreach ( $words as $word ) {
235 if ( ! empty( $word[ 0 ] ) ) {
236 $ucwords .= preg_match( '/[\p{L}]/u', $word[0] ) ? mb_strtoupper( $word[0], 'UTF-8' ) . mb_substr( $word, 1, mb_strlen( $word ), 'UTF-8' ) : $word;
237 }
238 }
239
240 return $ucwords;
241 }
242 }
243