{"id":48454,"date":"2024-12-02T14:39:21","date_gmt":"2024-12-02T06:39:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48454\/"},"modified":"2024-12-02T14:39:21","modified_gmt":"2024-12-02T06:39:21","slug":"%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96php%e5%87%bd%e6%95%b0%e4%b8%ad%e7%9a%84%e8%a7%a3%e6%9e%90%e6%93%8d%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48454\/","title":{"rendered":"\u5982\u4f55\u4f18\u5316php\u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f18\u5316php\u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u4f18\u5316php\u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u4f18\u5316 PHP \u51fd\u6570\u89e3\u6790\u64cd\u4f5c\u7684\u5efa\u8bae\uff1a\u7f13\u5b58\u89e3\u6790\u7ed3\u679c\uff0c\u4ee5\u51cf\u5c11\u91cd\u590d\u89e3\u6790\u65f6\u95f4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u89e3\u6790\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u5229\u7528 SimpleXML \u548c json_decode \u7b49\u5e93\u4f18\u5316 XML \u548c JSON \u89e3\u6790\u3002\u91c7\u7528 PHP \u6570\u7ec4\u6216\u5bf9\u8c61\u4ee3\u66ff\u89e3\u6790\uff0c\u4ee5\u63d0\u5347\u5185\u5b58\u7ba1\u7406\u6548\u7387\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730031229671e2e7d334cf.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f18\u5316php\u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f18\u5316php\u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316 PHP \u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u89e3\u6790\u64cd\u4f5c\u53ef\u80fd\u4ee3\u4ef7\u9ad8\u6602\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u578b\u6216\u590d\u6742\u7684\u6570\u636e\u96c6\u3002\u4e3a\u4e86\u4f18\u5316\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u6709\u51e0\u79cd\u6280\u672f\u53ef\u4ee5\u5e94\u7528\u3002<\/p>\n<p><strong>1. \u7f13\u5b58\u89e3\u6790\u7ed3\u679c<\/strong><\/p>\n<p>\u5982\u679c\u89e3\u6790\u64cd\u4f5c\u6d89\u53ca\u5230\u91cd\u590d\u4f7f\u7528\u76f8\u540c\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\u3002\u8fd9\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u89e3\u6790\u65f6\u95f4\uff0c\u7279\u522b\u662f\u5728\u591a\u6b21\u8c03\u7528\u540c\u4e00\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<pre>\/\/ \u7f13\u5b58\u6587\u4ef6\u7684\u89e3\u6790\u7ed3\u679c\n$cache = [];\nfunction parseFile($file) {\n  if (isset($cache[$file])) {\n    return $cache[$file];\n  }\n  $data = parse_ini_file($file);\n  $cache[$file] = $data;\n  return $data;\n}<\/pre>\n<p><strong>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u89e3\u6790<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u9ad8\u6548\u5730\u7528\u4e8e\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u6216\u9a8c\u8bc1\u7279\u5b9a\u6a21\u5f0f\u3002\u76f8\u6bd4\u4e8e\u9010\u5b57\u7b26\u89e3\u6790\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u5e38\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<pre>\/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece HTML \u4e2d\u63d0\u53d6\u94fe\u63a5\nfunction extractLinks($html) {\n  $matches = [];\n  preg_match_all('\/&lt;a href=\"([^\"]+)\"&gt;\/', $html, $matches);\n  return $matches[1];\n}<\/pre>\n<p><strong>3. \u4f18\u5316 XML \u548c JSON \u89e3\u6790<\/strong><\/p>\n<p>PHP \u5185\u7f6e\u4e86 XML \u89e3\u6790\u5668\u548c JSON \u89e3\u6790\u5668\uff0c\u4f46\u5b83\u4eec\u6bd4\u4f7f\u7528\u4e13\u95e8\u7684\u5e93\u901f\u5ea6\u6162\u3002\u8003\u8651\u4f7f\u7528\u5982 SimpleXML \u548c json_decode \u7b49\u5e93\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<pre>\/\/ \u4f7f\u7528 SimpleXML \u89e3\u6790 XML\n$xml = simplexml_load_file('file.xml');\nforeach ($xml-&gt;children() as $child) {\n  \/\/ ...\n}\n\n\/\/ \u4f7f\u7528 json_decode \u89e3\u6790 JSON\n$json = json_decode(file_get_contents('file.json'));\nforeach ($json-&gt;children as $child) {\n  \/\/ ...\n}<\/pre>\n<p><strong>4. \u4f7f\u7528\u6570\u7ec4\u548c\u5bf9\u8c61\u4ee3\u66ff\u89e3\u6790<\/strong><\/p>\n<p>\u89e3\u6790\u64cd\u4f5c\u901a\u5e38\u4f1a\u5c06\u6570\u636e\u5b58\u50a8\u5728\u54c8\u5e0c\u8868\u6216\u6570\u7ec4\u4e2d\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u76f4\u63a5\u4f7f\u7528 PHP \u6570\u7ec4\u6216\u5bf9\u8c61\u53ef\u80fd\u66f4\u6709\u6548\u7387\uff0c\u56e0\u4e3a\u5b83\u4eec\u5177\u6709\u66f4\u4f18\u5316\u7684\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n<pre>\/\/ \u4f7f\u7528\u6570\u7ec4\u4ee3\u66ff XML \u89e3\u6790\n$xml = simplexml_load_file('file.xml');\n$data = [];\nforeach ($xml-&gt;children() as $child) {\n  $data[$child-&gt;getName()] = $child;\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u89e3\u6790\u5927\u578b CSV \u6587\u4ef6<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u9700\u8981\u89e3\u6790\u5927\u578b CSV \u6587\u4ef6\u7684\u573a\u666f\u3002\u901a\u8fc7\u4f7f\u7528 SplFileObject \u548c fgetcsv \u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u8fd9\u79cd\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5f88\u8017\u65f6\u3002\u4e3a\u4e86\u4f18\u5316\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u5e76\u4f7f\u7528 str_getcsv \u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<pre>\/\/ \u4ece CSV \u6587\u4ef6\u52a0\u8f7d\u6570\u636e\u5230\u5185\u5b58\u4e2d\n$data = [];\n$file = new SplFileObject('large_csv.csv');\n$file-&gt;setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD);\nforeach ($file as $line) {\n  $data[] = $line;\n}\n\n\/\/ \u4f7f\u7528 str_getcsv \u89e3\u6790\u6570\u636e\n$records = [];\nforeach ($data as $row) {\n  $records[] = str_getcsv($row[0]);\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f18\u5316php\u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u4f18\u5316php\u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 \u4f18\u5316 PHP \u51fd\u6570\u89e3\u6790\u64cd\u4f5c\u7684\u5efa\u8bae\uff1a\u7f13\u5b58\u89e3\u6790\u7ed3\u679c\uff0c\u4ee5\u51cf\u5c11\u91cd\u590d\u89e3\u6790\u65f6\u95f4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u89e3\u6790\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u5229\u7528 SimpleXML \u548c json_decode \u7b49\u5e93\u4f18\u5316 XML \u548c JSON \u89e3\u6790\u3002\u91c7\u7528 PHP \u6570\u7ec4\u6216\u5bf9\u8c61\u4ee3\u66ff\u89e3\u6790\uff0c\u4ee5\u63d0\u5347\u5185\u5b58\u7ba1\u7406\u6548\u7387\u3002 \u5982\u4f55\u4f18\u5316 PHP \u51fd\u6570\u4e2d\u7684\u89e3\u6790\u64cd\u4f5c\uff1f \u5728 PHP \u4e2d\uff0c\u89e3\u6790\u64cd\u4f5c\u53ef\u80fd\u4ee3\u4ef7\u9ad8\u6602\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u578b\u6216\u590d\u6742\u7684\u6570\u636e\u96c6\u3002\u4e3a\u4e86\u4f18\u5316\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u6709\u51e0\u79cd\u6280\u672f\u53ef\u4ee5\u5e94\u7528\u3002 1. \u7f13\u5b58\u89e3\u6790\u7ed3\u679c \u5982\u679c\u89e3\u6790\u64cd\u4f5c\u6d89\u53ca\u5230\u91cd\u590d\u4f7f\u7528\u76f8\u540c\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\u3002\u8fd9\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u89e3\u6790\u65f6\u95f4\uff0c\u7279\u522b\u662f\u5728\u591a\u6b21\u8c03\u7528\u540c\u4e00\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u3002 \/\/ \u7f13\u5b58\u6587\u4ef6\u7684\u89e3\u6790\u7ed3\u679c $cache = []; function parseFile($file) { if (isset($cache[$file])) { return $cache[$file]; } $data = parse_ini_file($file); $cache[$file] = $data; return $data; } 2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u89e3\u6790 \u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u9ad8\u6548\u5730\u7528\u4e8e\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u6216\u9a8c\u8bc1\u7279\u5b9a\u6a21\u5f0f\u3002\u76f8\u6bd4\u4e8e\u9010\u5b57\u7b26\u89e3\u6790\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u5e38\u901f\u5ea6\u66f4\u5feb\u3002 \/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece HTML \u4e2d\u63d0\u53d6\u94fe\u63a5 function [&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-48454","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48454","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=48454"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48454\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}