{"id":23970,"date":"2024-11-21T16:42:33","date_gmt":"2024-11-21T08:42:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23970\/"},"modified":"2024-11-21T16:42:33","modified_gmt":"2024-11-21T08:42:33","slug":"yii%e6%a1%86%e6%9e%b6%e4%b8%ad%e7%9a%84%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%9a%e5%ae%9e%e7%8e%b0%e9%ab%98%e6%95%88%e7%9a%84%e6%96%87%e6%9c%ac%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23970\/","title":{"rendered":"Yii\u6846\u67b6\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u5b9e\u73b0\u9ad8\u6548\u7684\u6587\u672c\u64cd\u4f5c"},"content":{"rendered":"<p>yii\u6846\u67b6\u662f\u4e00\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u6587\u672c\u6570\u636e\uff0c\u5176\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5176\u6838\u5fc3\u90e8\u5206\u4e4b\u4e00\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8yii\u6846\u67b6\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u4ee5\u53ca\u5982\u4f55\u5b9e\u73b0\u9ad8\u6548\u7684\u6587\u672c\u64cd\u4f5c\u3002<\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5339\u914d\u7279\u5b9a\u7684\u6a21\u5f0f\uff0c\u5e76\u5bf9\u7b26\u5408\u6761\u4ef6\u7684\u6587\u672c\u8fdb\u884c\u5904\u7406\u3002\u5728Yii\u6846\u67b6\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u8868\u5355\u8f93\u5165\u3001\u89e3\u6790\u6570\u636e\u3001\u641c\u7d22\u3001\u66ff\u6362\u7b49\u591a\u79cd\u573a\u666f\u4e2d\u3002<\/p>\n<p>Yii\u6846\u67b6\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e3b\u8981\u65b9\u5f0f\u662f\u901a\u8fc7preg_match()\u3001preg_match_all()\u3001preg_replace()\u548cpreg_split()\u8fd9\u51e0\u4e2aPHP\u51fd\u6570\u3002\u5176\u4e2d\uff0cpreg_match()\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u5355\u4e2a\u6587\u672c\u662f\u5426\u7b26\u5408\u67d0\u79cd\u89c4\u5219\uff0cpreg_match_all()\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u591a\u4e2a\u6587\u672c\uff0c\u5e76\u8fd4\u56de\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c\uff0cpreg_replace()\u53ef\u4ee5\u7528\u4e8e\u66ff\u6362\u7b26\u5408\u6761\u4ef6\u7684\u6587\u672c\uff0cpreg_split()\u53ef\u4ee5\u7528\u4e8e\u6309\u7167\u67d0\u79cd\u6a21\u5f0f\u5c06\u6587\u672c\u5206\u5272\u6210\u6570\u7ec4\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528preg_match()\u6765\u9a8c\u8bc1\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6570\u5b57\u548c\u5b57\u6bcd\uff1a<\/p>\n<pre>$pattern = '\/^[a-zA-Z0-9]+$\/';\n$text = 'Hello123';\n\nif (preg_match($pattern, $text)) {\n    echo 'The text contains only letters and digits.';\n} else {\n    echo 'The text contains other characters.';\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u5b83\u53ea\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u5e76\u4f7f\u7528preg_match()\u51fd\u6570\u5bf9$text\u53d8\u91cf\u8fdb\u884c\u9a8c\u8bc1\u3002\u5982\u679c\u9a8c\u8bc1\u901a\u8fc7\uff0c\u5c31\u8f93\u51fa&#8221;The text contains only letters and digits.&#8221;\uff0c\u5426\u5219\u8f93\u51fa&#8221;The text contains other characters.&#8221;\u3002<\/p>\n<p>\u9664\u4e86preg_match()\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528preg_match_all()\u51fd\u6570\u6765\u5339\u914d\u591a\u4e2a\u6587\u672c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>$pattern = '\/[0-9]+\/';\n$text = 'I have 2 apples and 3 oranges.';\n\n$count = preg_match_all($pattern, $text, $matches);\n\necho 'There are ' . $count . ' numbers in the text: ' . implode(',', $matches[0]);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5339\u914d\u6240\u6709\u7684\u6570\u5b57\uff0c\u5e76\u4f7f\u7528preg_match_all()\u51fd\u6570\u5bf9$text\u53d8\u91cf\u8fdb\u884c\u5339\u914d\uff0c\u6700\u540e\u8f93\u51fa\u5339\u914d\u5230\u7684\u6570\u5b57\u603b\u6570\u4ee5\u53ca\u5b83\u4eec\u7684\u503c\u3002<\/p>\n<p>\u9664\u4e86\u4ee5\u4e0a\u4e24\u4e2a\u51fd\u6570\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528preg_replace()\u51fd\u6570\u6765\u66ff\u6362\u7b26\u5408\u6761\u4ef6\u7684\u6587\u672c\u3002\u4f8b\u5982\uff0c<\/p>\n<pre>$pattern = '\/s+\/';\n$text = 'This is a sentence with spaces.';\n\n$newText = preg_replace($pattern, '-', $text);\n\necho $newText;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5339\u914d\u6240\u6709\u7684\u7a7a\u683c\uff0c\u5e76\u4f7f\u7528preg_replace()\u51fd\u6570\u5c06\u5176\u4e2d\u7684\u7a7a\u683c\u66ff\u6362\u4e3a&#8221;-&#8220;\uff0c\u6700\u540e\u8f93\u51fa\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e00\u79cd\u6bd4\u8f83\u7279\u6b8a\u7684\u7528\u6cd5\u2014\u2014preg_split()\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u6309\u7167\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u5c06\u6587\u672c\u5206\u5272\u6210\u6570\u7ec4\u3002\u4f8b\u5982\uff0c<\/p>\n<pre>$pattern = '\/W+\/';\n$text = 'Hello, world!';\n\n$words = preg_split($pattern, $text);\n\nprint_r($words);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5339\u914d\u9664\u4e86\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\u4ee5\u5916\u7684\u6240\u6709\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528preg_split()\u51fd\u6570\u5c06$text\u6587\u672c\u6839\u636e\u8be5\u6a21\u5f0f\u5206\u5272\u6210\u6570\u7ec4\u3002\u6700\u540e\u8f93\u51fa\u6240\u6709\u7684\u5355\u8bcd\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u5728Yii\u6846\u67b6\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u9ad8\u6548\u7684\u6587\u672c\u64cd\u4f5c\u3002\u901a\u8fc7\u719f\u7ec3\u638c\u63e1\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u6211\u4eec\u7684\u6587\u672c\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fYii\u6846\u67b6\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u5b9e\u73b0\u9ad8\u6548\u7684\u6587\u672c\u64cd\u4f5c\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>yii\u6846\u67b6\u662f\u4e00\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u6587\u672c\u6570\u636e\uff0c\u5176\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5176\u6838\u5fc3\u90e8\u5206\u4e4b\u4e00\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8yii\u6846\u67b6\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u4ee5\u53ca\u5982\u4f55\u5b9e\u73b0\u9ad8\u6548\u7684\u6587\u672c\u64cd\u4f5c\u3002 \u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5339\u914d\u7279\u5b9a\u7684\u6a21\u5f0f\uff0c\u5e76\u5bf9\u7b26\u5408\u6761\u4ef6\u7684\u6587\u672c\u8fdb\u884c\u5904\u7406\u3002\u5728Yii\u6846\u67b6\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u8868\u5355\u8f93\u5165\u3001\u89e3\u6790\u6570\u636e\u3001\u641c\u7d22\u3001\u66ff\u6362\u7b49\u591a\u79cd\u573a\u666f\u4e2d\u3002 Yii\u6846\u67b6\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e3b\u8981\u65b9\u5f0f\u662f\u901a\u8fc7preg_match()\u3001preg_match_all()\u3001preg_replace()\u548cpreg_split()\u8fd9\u51e0\u4e2aPHP\u51fd\u6570\u3002\u5176\u4e2d\uff0cpreg_match()\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u5355\u4e2a\u6587\u672c\u662f\u5426\u7b26\u5408\u67d0\u79cd\u89c4\u5219\uff0cpreg_match_all()\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u591a\u4e2a\u6587\u672c\uff0c\u5e76\u8fd4\u56de\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c\uff0cpreg_replace()\u53ef\u4ee5\u7528\u4e8e\u66ff\u6362\u7b26\u5408\u6761\u4ef6\u7684\u6587\u672c\uff0cpreg_split()\u53ef\u4ee5\u7528\u4e8e\u6309\u7167\u67d0\u79cd\u6a21\u5f0f\u5c06\u6587\u672c\u5206\u5272\u6210\u6570\u7ec4\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528preg_match()\u6765\u9a8c\u8bc1\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6570\u5b57\u548c\u5b57\u6bcd\uff1a $pattern = &#8216;\/^[a-zA-Z0-9]+$\/&#8217;; $text = &#8216;Hello123&#8217;; if (preg_match($pattern, $text)) { echo &#8216;The text contains only letters and digits.&#8217;; } else { echo &#8216;The text contains other characters.&#8217;; } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u5b83\u53ea\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u5e76\u4f7f\u7528preg_match()\u51fd\u6570\u5bf9$text\u53d8\u91cf\u8fdb\u884c\u9a8c\u8bc1\u3002\u5982\u679c\u9a8c\u8bc1\u901a\u8fc7\uff0c\u5c31\u8f93\u51fa&#8221;The text contains only letters and digits.&#8221;\uff0c\u5426\u5219\u8f93\u51fa&#8221;The text contains other characters.&#8221;\u3002 \u9664\u4e86preg_match()\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528preg_match_all()\u51fd\u6570\u6765\u5339\u914d\u591a\u4e2a\u6587\u672c\uff0c\u4f8b\u5982\uff1a $pattern = &#8216;\/[0-9]+\/&#8217;; $text = &#8216;I have 2 apples and [&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-23970","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23970","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=23970"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23970\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}