{"id":22391,"date":"2024-11-21T11:31:25","date_gmt":"2024-11-21T03:31:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22391\/"},"modified":"2024-11-21T11:31:25","modified_gmt":"2024-11-21T03:31:25","slug":"laravel%e6%80%8e%e4%b9%88%e6%88%aa%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22391\/","title":{"rendered":"laravel\u600e\u4e48\u622a\u53d6\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>laravel\u662f\u4e00\u6b3e\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u7b80\u5316\u4e86web\u5f00\u53d1\u7684\u8bb8\u591a\u65b9\u9762\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5904\u7406\uff0c\u5176\u4e2d\u622a\u53d6\u5b57\u7b26\u4e32\u662f\u6700\u5e38\u89c1\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002laravel\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5e2e\u52a9\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728laravel\u4e2d\u622a\u53d6\u5b57\u7b26\u4e32\u3002<\/p>\n<ol>\n<li>PHP\u5185\u7f6e\u51fd\u6570<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\u622a\u53d6\u5b57\u7b26\u4e32\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528PHP\u5185\u7f6e\u51fd\u6570\uff0c\u5982substr()\u548cmb_substr()\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u524d\u8005\u5047\u5b9a\u5b57\u7b26\u4e32\u4e3a\u5355\u5b57\u8282\u5b57\u7b26\u96c6\uff0c\u540e\u8005\u5219\u652f\u6301\u591a\u5b57\u8282\u5b57\u7b26\u96c6\u3002\u4e0b\u9762\u662f\u4e24\u4e2a\u51fd\u6570\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre>\/\/substr()\u51fd\u6570\u8bed\u6cd5\nsubstr(string $str, int $start, int $length = null): string|false\n\n\/\/mb_substr()\u51fd\u6570\u8bed\u6cd5\nmb_substr(string $str, int $start, int $length = null, string $encoding = null): string|false<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0c$str\u662f\u8981\u622a\u53d6\u7684\u5b57\u7b26\u4e32\uff0c$start\u662f\u8d77\u59cb\u4f4d\u7f6e\uff0c$length\u662f\u8981\u622a\u53d6\u7684\u957f\u5ea6\uff08\u53ef\u9009\u53c2\u6570\uff09\uff0c$encoding\u662f\u5b57\u7b26\u7f16\u7801\uff08\u4ec5\u5728\u4f7f\u7528mb_substr()\u51fd\u6570\u65f6\u9700\u8981\uff09\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u8fd4\u56de\u622a\u53d6\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5931\u8d25\u5219\u8fd4\u56defalse\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u4ece\u5b57\u7b26\u4e32\u201cHello World\u201d\u4e2d\u63d0\u53d6\u524d5\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre>$str = \"Hello World\";\n$result = substr($str, 0, 5);\necho $result; \/\/\u8f93\u51fa\u201cHello\u201d<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u8981\u4f7f\u7528mb_substr()\u51fd\u6570\uff0c\u5219\u9700\u8981\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>$str = \"\u4f60\u597d\uff0c\u4e16\u754c\";\n$result = mb_substr($str, 0, 2, \"UTF-8\");\necho $result; \/\/\u8f93\u51fa\u201c\u4f60\u597d\u201d<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e24\u4e2a\u51fd\u6570\u5728Laravel\u4e2d\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u6709\u4e00\u4e9b\u9650\u5236\u3002\u9996\u5148\uff0c\u5b83\u4eec\u4e0d\u652f\u6301Unicode\u5b57\u7b26\u3002\u5176\u6b21\uff0c\u5982\u679c\u8981\u622a\u53d6\u7684\u957f\u5ea6\u8d85\u8fc7\u5b57\u7b26\u4e32\u7684\u5b9e\u9645\u957f\u5ea6\uff0c\u5219\u51fd\u6570\u4f1a\u8fd4\u56de\u6574\u4e2a\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528Laravel\u63d0\u4f9b\u7684\u8f85\u52a9\u51fd\u6570\u548c\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>Str\u52a9\u624b\u51fd\u6570<\/li>\n<\/ol>\n<p>Laravel\u7684Str\u52a9\u624b\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u7ec4\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u62ec\u622a\u53d6\u548c\u4fee\u526a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4f7f\u7528\u6d41\u7545\u7684\u8bed\u6cd5\u548c\u6613\u4e8e\u8bb0\u5fc6\u7684\u540d\u79f0\uff0c\u975e\u5e38\u9002\u5408\u5728Laravel\u9879\u76ee\u4e2d\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>Str::limit($value, $limit = 100, $end = &#8216;&#8230;&#8217;)<br \/>\u622a\u53d6\u5b57\u7b26\u4e32\u5e76\u5728\u7ed3\u5c3e\u6dfb\u52a0\u7701\u7565\u53f7<\/li>\n<\/ul>\n<pre>Str::limit('The quick brown fox jumps over the lazy dog', 20); \/\/ The quick brown fox...<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::substr($string, $start, $length = null)<br \/>\u622a\u53d6\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<pre>Str::substr('Hello world', 0, 5); \/\/ Hello<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::before($subject, $search)<br \/>\u5728\u6307\u5b9a\u5b57\u7b26\u4e32\u4e4b\u524d\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<pre>Str::before('hello world', 'world'); \/\/ hello<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::after($subject, $search)<br \/>\u5728\u6307\u5b9a\u5b57\u7b26\u4e32\u4e4b\u540e\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<pre>Str::after('hello world', 'hello '); \/\/ world<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::replaceFirst($search, $replace, $subject)<br \/>\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5b50\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u7ed9\u5b9a\u7684\u503c<\/li>\n<\/ul>\n<pre>Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog'); \/\/ a quick brown fox jumps over the lazy dog<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::replaceLast($search, $replace, $subject)<br \/>\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u51fa\u73b0\u7684\u5b50\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u7ed9\u5b9a\u7684\u503c<\/li>\n<\/ul>\n<pre>Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog'); \/\/ the quick brown fox jumps over a lazy dog<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::snake($value, $delimiter = &#8216;_&#8217;)<br \/>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u86c7\u5f62\uff08\u7528\u4e0b\u5212\u7ebf\u5206\u9694\u7684\u5355\u8bcd\uff09<\/li>\n<\/ul>\n<pre>Str::snake('HelloWorld'); \/\/ hello_world<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::camel($value)<br \/>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u9a7c\u5cf0\u5f0f<\/li>\n<\/ul>\n<pre>Str::camel('hello_world'); \/\/ helloWorld<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>Str::ucfirst($string)<br \/>\u5c06\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199<\/li>\n<\/ul>\n<pre>Str::ucfirst('hello world'); \/\/ Hello world<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>Laravel\u7684Str\u52a9\u624b\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55Laravel\u9879\u76ee\u4e2d\uff0c\u65e0\u9700\u8fdb\u884c\u989d\u5916\u7684\u914d\u7f6e\u548c\u5b89\u88c5\u3002\u6240\u6709\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\uff0c\u800c\u4e14\u5b83\u4eec\u4e0d\u9700\u8981\u8003\u8651\u5b57\u7b26\u7f16\u7801\u6216\u5b57\u7b26\u96c6\u7684\u95ee\u9898\u3002\u5b83\u4eec\u662f\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177\uff0c\u4f7f\u5b57\u7b26\u4e32\u64cd\u4f5c\u53d8\u5f97\u66f4\u5bb9\u6613\u548c\u53ef\u8bfb\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>Laravel\u63d0\u4f9b\u4e86\u591a\u79cd\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ecPHP\u5185\u7f6e\u51fd\u6570\u548cLaravel\u7684Str\u52a9\u624b\u51fd\u6570\u3002PHP\u5185\u7f6e\u51fd\u6570\u9650\u5236\u8f83\u591a\uff0c\u4e0d\u652f\u6301Unicode\u5b57\u7b26\uff0c\u5e76\u4e14\u9700\u8981\u624b\u52a8\u5904\u7406\u5b57\u7b26\u7f16\u7801\u7b49\u7ec6\u8282\u3002Laravel\u7684Str\u52a9\u624b\u51fd\u6570\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u548c\u5904\u7406\u529f\u80fd\uff0c\u4f7f\u7528\u7b80\u5355\u65b9\u4fbf\uff0c\u4e0d\u9700\u8981\u5904\u7406\u5b57\u7b26\u7f16\u7801\u7b49\u95ee\u9898\u3002\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u622a\u53d6\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u8d28\u91cf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u600e\u4e48\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>laravel\u662f\u4e00\u6b3e\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u7b80\u5316\u4e86web\u5f00\u53d1\u7684\u8bb8\u591a\u65b9\u9762\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5904\u7406\uff0c\u5176\u4e2d\u622a\u53d6\u5b57\u7b26\u4e32\u662f\u6700\u5e38\u89c1\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002laravel\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5e2e\u52a9\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728laravel\u4e2d\u622a\u53d6\u5b57\u7b26\u4e32\u3002 PHP\u5185\u7f6e\u51fd\u6570 \u5728Laravel\u4e2d\u622a\u53d6\u5b57\u7b26\u4e32\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528PHP\u5185\u7f6e\u51fd\u6570\uff0c\u5982substr()\u548cmb_substr()\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u524d\u8005\u5047\u5b9a\u5b57\u7b26\u4e32\u4e3a\u5355\u5b57\u8282\u5b57\u7b26\u96c6\uff0c\u540e\u8005\u5219\u652f\u6301\u591a\u5b57\u8282\u5b57\u7b26\u96c6\u3002\u4e0b\u9762\u662f\u4e24\u4e2a\u51fd\u6570\u7684\u8bed\u6cd5\uff1a \/\/substr()\u51fd\u6570\u8bed\u6cd5 substr(string $str, int $start, int $length = null): string|false \/\/mb_substr()\u51fd\u6570\u8bed\u6cd5 mb_substr(string $str, int $start, int $length = null, string $encoding = null): string|false \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c$str\u662f\u8981\u622a\u53d6\u7684\u5b57\u7b26\u4e32\uff0c$start\u662f\u8d77\u59cb\u4f4d\u7f6e\uff0c$length\u662f\u8981\u622a\u53d6\u7684\u957f\u5ea6\uff08\u53ef\u9009\u53c2\u6570\uff09\uff0c$encoding\u662f\u5b57\u7b26\u7f16\u7801\uff08\u4ec5\u5728\u4f7f\u7528mb_substr()\u51fd\u6570\u65f6\u9700\u8981\uff09\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u8fd4\u56de\u622a\u53d6\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5931\u8d25\u5219\u8fd4\u56defalse\u3002 \u4f8b\u5982\uff0c\u8981\u4ece\u5b57\u7b26\u4e32\u201cHello World\u201d\u4e2d\u63d0\u53d6\u524d5\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a $str = &#8220;Hello World&#8221;; $result = substr($str, 0, 5); echo $result; \/\/\u8f93\u51fa\u201cHello\u201d \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u8981\u4f7f\u7528mb_substr()\u51fd\u6570\uff0c\u5219\u9700\u8981\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\uff0c\u4f8b\u5982\uff1a $str = &#8220;\u4f60\u597d\uff0c\u4e16\u754c&#8221;; $result = mb_substr($str, 0, 2, &#8220;UTF-8&#8221;); echo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22391","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22391"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22391\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}