{"id":48661,"date":"2024-12-02T14:11:44","date_gmt":"2024-12-02T06:11:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48661\/"},"modified":"2024-12-02T14:11:44","modified_gmt":"2024-12-02T06:11:44","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%b5%8c%e5%a5%97%e6%a0%87%e7%ad%be%e7%9a%84%e5%ae%8c%e6%95%b4%e5%86%85%e5%ae%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48661\/","title":{"rendered":"\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/17301996486720c060e3761.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u83b7\u53d6\u5b57\u7b26\u4e32\u6307\u5b9a\u6807\u7b7e\u542b\u5d4c\u5957\u6807\u7b7e\u7684\u5185\u5bb9<\/strong><\/p>\n<p>\u5982\u4f55\u83b7\u53d6\u7c7b\u4f3c\u4ee5\u4e0b\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u5916\u5c42\u6240\u6709 &lt;if xx&gt;&lt;\/if&gt; \u6807\u7b7e\u53ca\u5176\u5305\u542b\u7684\u5185\u5bb9\uff1f<\/p>\n<pre>$str = \"\n{if 'a'}\n    111111\n    {if 'c'}33333{\/if}\n    {if 'd'}44444{\/if}\n    ......\n{\/if}\n{if 'b'}22222{\/if}\n\";<\/pre>\n<p>\u671f\u671b\u5f97\u5230\uff1a<\/p>\n<pre>[\n\"{if 'a'}\n    111111\n    {if 'c'}33333{\/if}\n    {if 'd'}44444{\/if}\n    ......\n{\/if}\",\n\"{if 'b'}22222{\/if}\"\n]<\/pre>\n<p>\u867d\u7136\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u83b7\u53d6\u6807\u7b7e\u5185\u5bb9\uff0c\u4f46\u7531\u4e8e\u5d4c\u5957\u7684\u5b58\u5728\uff0c\u9700\u8981\u66f4\u590d\u6742\u7684\u5904\u7406\u3002<\/p>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/strong><\/p>\n<pre>$stack = [];\n$top = null;\n$result = [];\npreg_match_all('!({\/?if)!', $str, $matches, PREG_OFFSET_CAPTURE);\nforeach ($matches[0] as [$match, $offset]) {\n    if ($match === '{if') {\n        $stack[] = $offset;\n        if ($top === null) { $top = $offset; }\n    } else {\n        $pop = array_pop($stack);\n        if ($pop === null) { throw new Exception('\u8bed\u6cd5\u9519\u8bef\uff0c\u5b58\u5728\u591a\u4f59\u7684 {\/if} \u6807\u7b7e'); }\n        if (empty($stack)) {\n            $newOffset = $offset + strlen($match);\n            $result[] = substr($str, $top, $newOffset);\n            $top = $newOffset + 1;\n        }\n    }\n}\nif (!empty($stack)) { throw new Exception('\u8bed\u6cd5\u9519\u8bef\uff0c\u5b58\u5728\u672a\u95ed\u5408\u7684 {if} \u6807\u7b7e'); }\nvar_dump($result);<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5957\u6807\u7b7e\u7684\u5b8c\u6574\u5185\u5bb9\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u83b7\u53d6\u5b57\u7b26\u4e32\u6307\u5b9a\u6807\u7b7e\u542b\u5d4c\u5957\u6807\u7b7e\u7684\u5185\u5bb9 \u5982\u4f55\u83b7\u53d6\u7c7b\u4f3c\u4ee5\u4e0b\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u5916\u5c42\u6240\u6709 &lt;if xx&gt;&lt;\/if&gt; \u6807\u7b7e\u53ca\u5176\u5305\u542b\u7684\u5185\u5bb9\uff1f $str = &#8221; {if &#8216;a&#8217;} 111111 {if &#8216;c&#8217;}33333{\/if} {if &#8216;d&#8217;}44444{\/if} &#8230;&#8230; {\/if} {if &#8216;b&#8217;}22222{\/if} &#8220;; \u671f\u671b\u5f97\u5230\uff1a [ &#8220;{if &#8216;a&#8217;} 111111 {if &#8216;c&#8217;}33333{\/if} {if &#8216;d&#8217;}44444{\/if} &#8230;&#8230; {\/if}&#8221;, &#8220;{if &#8216;b&#8217;}22222{\/if}&#8221; ] \u867d\u7136\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u83b7\u53d6\u6807\u7b7e\u5185\u5bb9\uff0c\u4f46\u7531\u4e8e\u5d4c\u5957\u7684\u5b58\u5728\uff0c\u9700\u8981\u66f4\u590d\u6742\u7684\u5904\u7406\u3002 \u4ee3\u7801\u5b9e\u73b0\uff1a $stack = []; $top = null; $result = []; preg_match_all(&#8216;!({\/?if)!&#8217;, $str, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as [$match, [&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-48661","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48661","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=48661"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48661\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}