{"id":48392,"date":"2024-12-02T17:30:14","date_gmt":"2024-12-02T09:30:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48392\/"},"modified":"2024-12-02T17:30:14","modified_gmt":"2024-12-02T09:30:14","slug":"php%e5%87%bd%e6%95%b0%e7%89%88%e6%9c%ac%e7%ae%a1%e7%90%86%e4%b8%8e%e5%8d%87%e7%ba%a7%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48392\/","title":{"rendered":"php\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aphp\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>\u6700\u4f73\u5b9e\u8df5\uff1a\u7ba1\u7406 PHP \u51fd\u6570\u7248\u672c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002\u4f7f\u7528 composer \u6307\u5b9a\u4f9d\u8d56\u5173\u7cfb\u7684\u7248\u672c\u3002\u6e10\u8fdb\u5f0f\u5347\u7ea7\u51fd\u6570\u7248\u672c\uff0c\u6bcf\u6b21\u5347\u7ea7\u4e00\u4e2a\u7248\u672c\u3002\u4f7f\u7528\u6d4b\u8bd5\u7528\u4f8b\u9a8c\u8bc1\u5347\u7ea7\u540e\u7684\u529f\u80fd\u3002\u5728\u5206\u9636\u6bb5\u73af\u5883\u4e2d\u6d4b\u8bd5\u5347\u7ea7\uff0c\u518d\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730022822671e0da6a6c9f.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" alt=\"php\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u5728\u7ef4\u62a4 PHP \u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u7ba1\u7406\u51fd\u6570\u7248\u672c\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u7ba1\u7406 PHP \u51fd\u6570\u7248\u672c\u5e76\u6267\u884c\u5e73\u6ed1\u7684\u5347\u7ea7\u3002<\/p>\n<p><strong>\u7248\u672c\u7ba1\u7406<\/strong><\/p>\n<ul>\n<li><strong>\u4f7f\u7528 composer \u5305\u7ba1\u7406\u5668\uff1a<\/strong>composer \u5141\u8bb8\u60a8\u6307\u5b9a\u4f9d\u8d56\u5173\u7cfb\u7684\u7279\u5b9a\u7248\u672c\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u59cb\u7ec8\u4f7f\u7528\u9884\u671f\u7684\u51fd\u6570\u7248\u672c\u3002<\/li>\n<li><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u81ea\u52a8\u52a0\u8f7d\u7a0b\u5e8f\uff1a<\/strong>\u5982\u679c\u60a8\u65e0\u6cd5\u4f7f\u7528 composer\uff0c\u8bf7\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u81ea\u52a8\u52a0\u8f7d\u7a0b\u5e8f\u4ee5\u6307\u5b9a\u51fd\u6570\u7684\u7279\u5b9a\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><strong>\u5347\u7ea7\u7b56\u7565<\/strong><\/p>\n<ul>\n<li><strong>\u6e10\u8fdb\u5f0f\u5347\u7ea7\uff1a<\/strong>\u4e00\u6b21\u5347\u7ea7\u4e00\u4e2a\u7248\u672c\uff0c\u4ee5\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u6f5c\u5728\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/li>\n<li><strong>\u6d4b\u8bd5\u9a71\u52a8\u7684\u5347\u7ea7\uff1a<\/strong>\u5728\u5347\u7ea7\u4e4b\u524d\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u4ee5\u9a8c\u8bc1\u65b0\u7248\u672c\u7684\u529f\u80fd\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/li>\n<li><strong>\u5efa\u7acb\u5206\u9636\u6bb5\u73af\u5883\uff1a<\/strong>\u5728\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e4b\u524d\uff0c\u5728\u5206\u9636\u6bb5\u73af\u5883\u4e2d\u6d4b\u8bd5\u5347\u7ea7\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u573a\u666f\uff1a<\/strong>\u9700\u8981\u5c06 <code>str_replace()<\/code> \u51fd\u6570\u5347\u7ea7\u5230 PHP 8.1 \u7248\u672c\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\u5728 composer.json \u6587\u4ef6\u4e2d\u66f4\u65b0\u4f9d\u8d56\u5173\u7cfb\uff1a<\/li>\n<\/ol>\n<pre>{\n  \"require\": {\n    \"symfony\/polyfill-php81\": \"^1.25\"\n  }\n}<\/pre>\n<ol>\n<li>\u8fd0\u884c <code>composer update<\/code> \u547d\u4ee4\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u6765\u9a8c\u8bc1\u5347\u7ea7\u540e\u7684\u884c\u4e3a\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php\n$originalString = 'Hello World';\n$replacedString = str_replace('World', 'Universe', $originalString);\n\nassertEquals('Hello Universe', $replacedString);<\/pre>\n<ol>\n<li>\u5728\u5206\u9636\u6bb5\u73af\u5883\u4e2d\u90e8\u7f72\u5347\u7ea7\u5e76\u8fd0\u884c\u6d4b\u8bd5\u3002<\/li>\n<li>\u5982\u679c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u5219\u5c06\u5347\u7ea7\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u3002<\/li>\n<\/ol>\n<p><strong>\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u5c06\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u7ba1\u7406 PHP \u51fd\u6570\u7248\u672c\u5e76\u786e\u4fdd\u5347\u7ea7\u8fc7\u7a0b\u7684\u5e73\u7a33\u6027\u3002<\/strong><\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aphp\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5 \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aphp\u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 \u6700\u4f73\u5b9e\u8df5\uff1a\u7ba1\u7406 PHP \u51fd\u6570\u7248\u672c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002\u4f7f\u7528 composer \u6307\u5b9a\u4f9d\u8d56\u5173\u7cfb\u7684\u7248\u672c\u3002\u6e10\u8fdb\u5f0f\u5347\u7ea7\u51fd\u6570\u7248\u672c\uff0c\u6bcf\u6b21\u5347\u7ea7\u4e00\u4e2a\u7248\u672c\u3002\u4f7f\u7528\u6d4b\u8bd5\u7528\u4f8b\u9a8c\u8bc1\u5347\u7ea7\u540e\u7684\u529f\u80fd\u3002\u5728\u5206\u9636\u6bb5\u73af\u5883\u4e2d\u6d4b\u8bd5\u5347\u7ea7\uff0c\u518d\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u3002 PHP \u51fd\u6570\u7248\u672c\u7ba1\u7406\u4e0e\u5347\u7ea7\u6700\u4f73\u5b9e\u8df5 \u5728\u7ef4\u62a4 PHP \u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u7ba1\u7406\u51fd\u6570\u7248\u672c\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u7ba1\u7406 PHP \u51fd\u6570\u7248\u672c\u5e76\u6267\u884c\u5e73\u6ed1\u7684\u5347\u7ea7\u3002 \u7248\u672c\u7ba1\u7406 \u4f7f\u7528 composer \u5305\u7ba1\u7406\u5668\uff1acomposer \u5141\u8bb8\u60a8\u6307\u5b9a\u4f9d\u8d56\u5173\u7cfb\u7684\u7279\u5b9a\u7248\u672c\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u59cb\u7ec8\u4f7f\u7528\u9884\u671f\u7684\u51fd\u6570\u7248\u672c\u3002 \u521b\u5efa\u81ea\u5b9a\u4e49\u81ea\u52a8\u52a0\u8f7d\u7a0b\u5e8f\uff1a\u5982\u679c\u60a8\u65e0\u6cd5\u4f7f\u7528 composer\uff0c\u8bf7\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u81ea\u52a8\u52a0\u8f7d\u7a0b\u5e8f\u4ee5\u6307\u5b9a\u51fd\u6570\u7684\u7279\u5b9a\u8def\u5f84\u3002 \u5347\u7ea7\u7b56\u7565 \u6e10\u8fdb\u5f0f\u5347\u7ea7\uff1a\u4e00\u6b21\u5347\u7ea7\u4e00\u4e2a\u7248\u672c\uff0c\u4ee5\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u6f5c\u5728\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002 \u6d4b\u8bd5\u9a71\u52a8\u7684\u5347\u7ea7\uff1a\u5728\u5347\u7ea7\u4e4b\u524d\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u4ee5\u9a8c\u8bc1\u65b0\u7248\u672c\u7684\u529f\u80fd\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\u3002 \u5efa\u7acb\u5206\u9636\u6bb5\u73af\u5883\uff1a\u5728\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e4b\u524d\uff0c\u5728\u5206\u9636\u6bb5\u73af\u5883\u4e2d\u6d4b\u8bd5\u5347\u7ea7\u3002 \u5b9e\u6218\u6848\u4f8b \u573a\u666f\uff1a\u9700\u8981\u5c06 str_replace() \u51fd\u6570\u5347\u7ea7\u5230 PHP 8.1 \u7248\u672c\u3002 \u6b65\u9aa4\uff1a \u5728 composer.json \u6587\u4ef6\u4e2d\u66f4\u65b0\u4f9d\u8d56\u5173\u7cfb\uff1a { &#8220;require&#8221;: { &#8220;symfony\/polyfill-php81&#8221;: &#8220;^1.25&#8221; } } \u8fd0\u884c composer update \u547d\u4ee4\u3002 \u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u6765\u9a8c\u8bc1\u5347\u7ea7\u540e\u7684\u884c\u4e3a\uff1a &lt;?php $originalString = &#8216;Hello World&#8217;; $replacedString = [&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-48392","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48392","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=48392"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48392\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}