laravelÔõô½ØÈ¡×Ö·û´®
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü£¬Ëü¼ò»¯ÁËweb¿ª·¢µÄÐí¶à·½Ãæ¡£ÔÚ¿ª·¢Àú³ÌÖУ¬¾³£ÐèÒª¶Ô×Ö·û´®¾ÙÐд¦Àí£¬ÆäÖнØÈ¡×Ö·û´®ÊÇ×î³£¼ûµÄ²Ù×÷Ö®Ò»¡£laravelÌṩÁËһϵÁÐ×ÊÖúº¯ÊýºÍÒªÁìÀ´½ØÈ¡×Ö·û´®£¬±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖнØÈ¡×Ö·û´®¡£
PHPÄÚÖú¯Êý
ÔÚLaravelÖнØÈ¡×Ö·û´®×î¼òÆÓµÄÒªÁìÊÇʹÓÃPHPÄÚÖú¯Êý£¬Èçsubstr()ºÍmb_substr()¡£ÕâÁ½¸öº¯Êý¶¼¿ÉÒÔ½ØÈ¡×Ö·û´®£¬Ç°Õß¼Ù¶¨×Ö·û´®Îªµ¥×Ö½Ú×Ö·û¼¯£¬ºóÕßÔòÖ§³Ö¶à×Ö½Ú×Ö·û¼¯¡£ÏÂÃæÊÇÁ½¸öº¯ÊýµÄÓï·¨£º
//substr()º¯ÊýÓï·¨ substr(string $str, int $start, int $length = null): string|false //mb_substr()º¯ÊýÓï·¨ mb_substr(string $str, int $start, int $length = null, string $encoding = null): string|false
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬$strÊÇÒª½ØÈ¡µÄ×Ö·û´®£¬$startÊÇÆðʼλÖã¬$lengthÊÇÒª½ØÈ¡µÄ³¤¶È£¨¿ÉÑ¡²ÎÊý£©£¬$encodingÊÇ×Ö·û±àÂ루½öÔÚʹÓÃmb_substr()º¯ÊýʱÐèÒª£©¡£ÕâÁ½¸öº¯Êý·µ»Ø½ØÈ¡ºóµÄ×Ö·û´®£¬ÈôÊÇʧ°ÜÔò·µ»Øfalse¡£
ÀýÈ磬Ҫ´Ó×Ö·û´®¡°Hello World¡±ÖÐÌáÈ¡Ç°5¸ö×Ö·û£¬¿ÉÒÔÕâÑùд£º
$str = "Hello World"; $result = substr($str, 0, 5); echo $result; //Êä³ö¡°Hello¡±
µÇ¼ºó¸´ÖÆ
ÈôÊÇҪʹÓÃmb_substr()º¯Êý£¬ÔòÐèÒªÖ¸¶¨×Ö·û±àÂ룬ÀýÈ磺
$str = "ÄãºÃ£¬ÌìÏÂ"; $result = mb_substr($str, 0, 2, "UTF-8"); echo $result; //Êä³ö¡°ÄãºÃ¡±
µÇ¼ºó¸´ÖÆ
ÕâÁ½¸öº¯ÊýÔÚLaravelÖÐʹÓúÜÊǼòÆÓ£¬µ«ÓÐһЩÏÞÖÆ¡£Ê×ÏÈ£¬ËüÃDz»Ö§³ÖUnicode×Ö·û¡£Æä´Î£¬ÈôÊÇÒª½ØÈ¡µÄ³¤¶ÈÁè¼Ý×Ö·û´®µÄÏÖʵ³¤¶È£¬Ôòº¯Êý»á·µ»ØÕû¸ö×Ö·û´®¡£Òò´Ë£¬ÈôÊÇÐèÒª¸ü¸ß¼¶µÄ×Ö·û´®½ØÈ¡¹¦Ð§£¬¿ÉÒÔʹÓÃLaravelÌṩµÄ¸¨Öúº¯ÊýºÍÒªÁì¡£
StrÖúÊÖº¯Êý
LaravelµÄStrÖúÊÖº¯ÊýÌṩÁËÒ»×éÀû±ãµÄÒªÁìÀ´´¦Àí×Ö·û´®£¬ÆäÖаüÀ¨½ØÈ¡ºÍÐÞ¼ô×Ö·û´®µÄÒªÁì¡£ÕâЩҪÁìʹÓÃÁ÷ͨµÄÓï·¨ºÍÒ×ÓÚÓ°ÏóµÄÃû³Æ£¬ºÜÊÇÊʺÏÔÚLaravelÏîÄ¿ÖÐʹÓá£ÏÂÃæÊÇһЩ³£ÓõÄÒªÁ죺
Str::limit($value, $limit = 100, $end = ‘…’)
½ØÈ¡×Ö·û´®²¢ÔÚ×îºóÌí¼ÓÊ¡ÂÔºÅ
Str::limit('The quick brown fox jumps over the lazy dog', 20); // The quick brown fox...
µÇ¼ºó¸´ÖÆ
Str::substr($string, $start, $length = null)
½ØÈ¡×Ö·û´®
Str::substr('Hello world', 0, 5); // Hello
µÇ¼ºó¸´ÖÆ
Str::before($subject, $search)
ÔÚÖ¸¶¨×Ö·û´®Ö®Ç°·µ»Ø×Ó×Ö·û´®
Str::before('hello world', 'world'); // hello
µÇ¼ºó¸´ÖÆ
Str::after($subject, $search)
ÔÚÖ¸¶¨×Ö·û´®Ö®ºó·µ»Ø×Ó×Ö·û´®
Str::after('hello world', 'hello '); // world
µÇ¼ºó¸´ÖÆ
Str::replaceFirst($search, $replace, $subject)
½«×Ö·û´®ÖеĵÚÒ»¸ö·ºÆðµÄ×Ó×Ö·û´®Ì滻Ϊ¸ø¶¨µÄÖµ
Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog'); // a quick brown fox jumps over the lazy dog
µÇ¼ºó¸´ÖÆ
Str::replaceLast($search, $replace, $subject)
½«×Ö·û´®ÖеÄ×îºóÒ»¸ö·ºÆðµÄ×Ó×Ö·û´®Ì滻Ϊ¸ø¶¨µÄÖµ
Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog'); // the quick brown fox jumps over a lazy dog
µÇ¼ºó¸´ÖÆ
Str::snake($value, $delimiter = ‘_’)
½«×Ö·û´®×ª»»ÎªÉßÐΣ¨ÓÃÏ»®ÏßÍÑÀëµÄµ¥´Ê£©
Str::snake('HelloWorld'); // hello_world
µÇ¼ºó¸´ÖÆ
Str::camel($value)
½«×Ö·û´®×ª»»ÎªÍÕ·åʽ
Str::camel('hello_world'); // helloWorld
µÇ¼ºó¸´ÖÆ
Str::ucfirst($string)
½«×Ö·û´®µÄµÚÒ»¸ö×Ö·ûת»»Îª´óд
Str::ucfirst('hello world'); // Hello world
µÇ¼ºó¸´ÖÆ
LaravelµÄStrÖúÊÖº¯Êý¿ÉÒÔÓÃÓÚÈκÎLaravelÏîÄ¿ÖУ¬ÎÞÐè¾ÙÐÐÌØÁíÍâÉèÖúÍ×°Öá£ËùÓÐÕâЩҪÁ춼¿ÉÒÔÖ±½ÓŲÓ㬲¢ÇÒËüÃDz»ÐèҪ˼Á¿×Ö·û±àÂë»ò×Ö·û¼¯µÄÎÊÌâ¡£ËüÃÇÊÇÒ»¸öÓÐÓõŤ¾ß£¬Ê¹×Ö·û´®²Ù×÷±äµÃ¸üÈÝÒ׺ͿɶÁ¡£
×ܽá
LaravelÌṩÁ˶àÖÖ½ØÈ¡×Ö·û´®µÄÒªÁ죬ÆäÖаüÀ¨PHPÄÚÖú¯ÊýºÍLaravelµÄStrÖúÊÖº¯Êý¡£PHPÄÚÖú¯ÊýÏÞÖƽ϶࣬²»Ö§³ÖUnicode×Ö·û£¬²¢ÇÒÐèÒªÊÖ¶¯´¦Àí×Ö·û±àÂëµÈϸ½Ú¡£LaravelµÄStrÖúÊÖº¯ÊýÔòÌṩÁ˸ü¸ß¼¶µÄ×Ö·û´®½ØÈ¡ºÍ´¦Àí¹¦Ð§£¬Ê¹ÓüòÆÓÀû±ã£¬²»ÐèÒª´¦Àí×Ö·û±àÂëµÈÎÊÌâ¡£ÔÚÈÕ³£¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔÑ¡ÔñÊʺÏ×Ô¼ºµÄ½ØÈ¡×Ö·û´®ÒªÁ죬Ìá¸ß¿ª·¢Ð§ÂʺÍÖÊÁ¿¡£
ÒÔÉϾÍÊÇlaravelÔõô½ØÈ¡×Ö·û´®µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡