Overview

Namespaces

  • Composer
    • Autoload
  • Illuminate
    • Support
      • Contracts
      • Facades
  • Laravella
    • Cart
      • Facades
  • None
  • PHP

Classes

  • ClassLoader
  • Collection
  • Fluent
  • Manager
  • MessageBag
  • NamespacedItemResolver
  • Pluralizer
  • SerializableClosure
  • ServiceProvider
  • Str
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php namespace Illuminate\Support;
  2: 
  3: class Str {
  4: 
  5:     /**
  6:      * The registered string macros.
  7:      *
  8:      * @var array
  9:      */
 10:     protected static $macros = array();
 11: 
 12:     /**
 13:      * Transliterate a UTF-8 value to ASCII.
 14:      *
 15:      * @param  string  $value
 16:      * @return string
 17:      */
 18:     public static function ascii($value)
 19:     {
 20:         return \Patchwork\Utf8::toAscii($value);
 21:     }
 22: 
 23:     /**
 24:      * Convert a value to camel case.
 25:      *
 26:      * @param  string  $value
 27:      * @return string
 28:      */
 29:     public static function camel($value)
 30:     {
 31:         return lcfirst(static::studly($value));
 32:     }
 33: 
 34:     /**
 35:      * Determine if a given string contains a given sub-string.
 36:      *
 37:      * @param  string        $haystack
 38:      * @param  string|array  $needle
 39:      * @return bool
 40:      */
 41:     public static function contains($haystack, $needle)
 42:     {
 43:         foreach ((array) $needle as $n)
 44:         {
 45:             if (strpos($haystack, $n) !== false) return true;
 46:         }
 47: 
 48:         return false;
 49:     }
 50: 
 51:     /**
 52:      * Determine if a given string ends with a given needle.
 53:      *
 54:      * @param string $haystack
 55:      * @param string|array $needles
 56:      * @return bool
 57:      */
 58:     public static function endsWith($haystack, $needles)
 59:     {
 60:         foreach ((array) $needles as $needle)
 61:         {
 62:             if ($needle == substr($haystack, strlen($haystack) - strlen($needle))) return true;
 63:         }
 64: 
 65:         return false;
 66:     }
 67: 
 68:     /**
 69:      * Cap a string with a single instance of a given value.
 70:      *
 71:      * @param  string  $value
 72:      * @param  string  $cap
 73:      * @return string
 74:      */
 75:     public static function finish($value, $cap)
 76:     {
 77:         return rtrim($value, $cap).$cap;
 78:     }
 79: 
 80:     /**
 81:      * Determine if a given string matches a given pattern.
 82:      *
 83:      * @param  string  $pattern
 84:      * @param  string  $value
 85:      * @return bool
 86:      */
 87:     public static function is($pattern, $value)
 88:     {
 89:         if ($pattern == $value) return true;
 90: 
 91:         $pattern = preg_quote($pattern, '#');
 92: 
 93:         // Asterisks are translated into zero-or-more regular expression wildcards
 94:         // to make it convenient to check if the strings starts with the given
 95:         // pattern such as "library/*", making any string check convenient.
 96:         if ($pattern !== '/')
 97:         {
 98:             $pattern = str_replace('\*', '.*', $pattern).'\z';
 99:         }
100:         else
101:         {
102:             $pattern = '/$';
103:         }
104: 
105:         return (bool) preg_match('#^'.$pattern.'#', $value);
106:     }
107: 
108:     /**
109:      * Return the length of the given string.
110:      *
111:      * @param  string  $value
112:      * @return int
113:      */
114:     public static function length($value)
115:     {
116:         return mb_strlen($value);
117:     }
118: 
119:     /**
120:      * Limit the number of characters in a string.
121:      *
122:      * @param  string  $value
123:      * @param  int     $limit
124:      * @param  string  $end
125:      * @return string
126:      */
127:     public static function limit($value, $limit = 100, $end = '...')
128:     {
129:         if (mb_strlen($value) <= $limit) return $value;
130: 
131:         return mb_substr($value, 0, $limit, 'UTF-8').$end;
132:     }
133: 
134:     /**
135:      * Convert the given string to lower-case.
136:      *
137:      * @param  string  $value
138:      * @return string
139:      */
140:     public static function lower($value)
141:     {
142:         return mb_strtolower($value);
143:     }
144: 
145:     /**
146:      * Limit the number of words in a string.
147:      *
148:      * @param  string  $value
149:      * @param  int     $words
150:      * @param  string  $end
151:      * @return string
152:      */
153:     public static function words($value, $words = 100, $end = '...')
154:     {
155:         preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
156: 
157:         if ( ! isset($matches[0])) return $value;
158: 
159:         if (strlen($value) == strlen($matches[0])) return $value;
160: 
161:         return rtrim($matches[0]).$end;
162:     }
163: 
164:     /**
165:      * Parse a Class@method style callback into class and method.
166:      *
167:      * @param  string  $callback
168:      * @param  string  $default
169:      * @return array
170:      */
171:     public static function parseCallback($callback, $default)
172:     {
173:         return static::contains($callback, '@') ? explode('@', $callback, 2) : array($callback, $default);
174:     }
175: 
176:     /**
177:      * Get the plural form of an English word.
178:      *
179:      * @param  string  $value
180:      * @param  int  $count
181:      * @return string
182:      */
183:     public static function plural($value, $count = 2)
184:     {
185:         return Pluralizer::plural($value, $count);
186:     }
187: 
188:     /**
189:      * Generate a more truly "random" alpha-numeric string.
190:      *
191:      * @param  int     $length
192:      * @return string
193:      */
194:     public static function random($length = 16)
195:     {
196:         if (function_exists('openssl_random_pseudo_bytes'))
197:         {
198:             $bytes = openssl_random_pseudo_bytes($length * 2);
199: 
200:             if ($bytes === false)
201:             {
202:                 throw new \RuntimeException('Unable to generate random string.');
203:             }
204: 
205:             return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length);
206:         }
207: 
208:         return static::quickRandom($length);
209:     }
210: 
211:     /**
212:      * Generate a "random" alpha-numeric string.
213:      *
214:      * Should not be considered sufficient for cryptography, etc.
215:      *
216:      * @param  int     $length
217:      * @return string
218:      */
219:     public static function quickRandom($length = 16)
220:     {
221:         $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
222: 
223:         return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
224:     }
225: 
226:     /**
227:      * Convert the given string to upper-case.
228:      *
229:      * @param  string  $value
230:      * @return string
231:      */
232:     public static function upper($value)
233:     {
234:         return mb_strtoupper($value);
235:     }
236: 
237:     /**
238:      * Get the singular form of an English word.
239:      *
240:      * @param  string  $value
241:      * @return string
242:      */
243:     public static function singular($value)
244:     {
245:         return Pluralizer::singular($value);
246:     }
247: 
248:     /**
249:      * Generate a URL friendly "slug" from a given string.
250:      *
251:      * @param  string  $title
252:      * @param  string  $separator
253:      * @return string
254:      */
255:     public static function slug($title, $separator = '-')
256:     {
257:         $title = static::ascii($title);
258: 
259:         // Remove all characters that are not the separator, letters, numbers, or whitespace.
260:         $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
261: 
262:         // Convert all dashes/undescores into separator
263:         $flip = $separator == '-' ? '_' : '-';
264: 
265:         $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
266: 
267:         // Replace all separator characters and whitespace by a single separator
268:         $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
269: 
270:         return trim($title, $separator);
271:     }
272: 
273:     /**
274:      * Convert a string to snake case.
275:      *
276:      * @param  string  $value
277:      * @param  string  $delimiter
278:      * @return string
279:      */
280:     public static function snake($value, $delimiter = '_')
281:     {
282:         $replace = '$1'.$delimiter.'$2';
283: 
284:         return ctype_lower($value) ? $value : strtolower(preg_replace('/(.)([A-Z])/', $replace, $value));
285:     }
286: 
287:     /**
288:      * Determine if a string starts with a given needle.
289:      *
290:      * @param  string  $haystack
291:      * @param  string|array  $needles
292:      * @return bool
293:      */
294:     public static function startsWith($haystack, $needles)
295:     {
296:         foreach ((array) $needles as $needle)
297:         {
298:             if (strpos($haystack, $needle) === 0) return true;
299:         }
300: 
301:         return false;
302:     }
303: 
304:     /**
305:      * Convert a value to studly caps case.
306:      *
307:      * @param  string  $value
308:      * @return string
309:      */
310:     public static function studly($value)
311:     {
312:         $value = ucwords(str_replace(array('-', '_'), ' ', $value));
313: 
314:         return str_replace(' ', '', $value);
315:     }
316: 
317:     /**
318:      * Register a custom string macro.
319:      *
320:      * @param  string    $name
321:      * @param  callable  $macro
322:      * @return void
323:      */
324:     public static function macro($name, $macro)
325:     {
326:         static::$macros[$name] = $macro;
327:     }
328: 
329:     /**
330:      * Dynamically handle calls to the string class.
331:      *
332:      * @param  string  $method
333:      * @param  array   $parameters
334:      * @return mixed
335:      */
336:     public static function __callStatic($method, $parameters)
337:     {
338:         if (isset(static::$macros[$method]))
339:         {
340:             return call_user_func_array(static::$macros[$method], $parameters);
341:         }
342: 
343:         throw new \BadMethodCallException("Method {$method} does not exist.");
344:     }
345: 
346: }
347: 
cart API documentation generated by ApiGen 2.8.0