{"id":77,"date":"2016-06-12T14:50:59","date_gmt":"2016-06-12T06:50:59","guid":{"rendered":"http:\/\/www.tra56.com\/?p=77"},"modified":"2016-06-12T14:51:50","modified_gmt":"2016-06-12T06:51:50","slug":"php-validation-email-url-ip-legal","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/77.html","title":{"rendered":"PHP\u9a8c\u8bc1\u90ae\u7bb1\u3001URL\u3001IP\u662f\u5426\u5408\u6cd5"},"content":{"rendered":"

\n\t\u4ee5\u524d\u7528PHP\u9a8c\u8bc1\u90ae\u7bb1\u3001URL\u3001IP\u662f\u5426\u5408\u6cd5\u90fd\u662f\u901a\u8fc7\u81ea\u5df1\u5199\u6b63\u5219\u6765\u5b9e\u73b0\uff0c\u4f46\u662f\u6709\u65f6\u5019\u8111\u5b50\u53d1\u660f\uff0c\u53ef\u80fd\u4f1a\u5199\u51fa\u4e00\u4e2a\u4e0d\u662f\u5b8c\u5168\u6b63\u786e\u7684\u6b63\u5219\uff0c\u5bfc\u81f4\u9a8c\u8bc1\u51fa\u9519\uff0c\u4eca\u5929\u53d1\u73b0\u539f\u6765PHP\u672c\u8eab\u81ea\u5e26\u4e86\u9a8c\u8bc1\u90ae\u7bb1\u3001URL\u3001IP\u662f\u5426\u5408\u6cd5\u7684\u51fd\u6570\u3002\n<\/p>\n

\n\t\u4e3b\u8981\u4f7f\u7528\u7684\u662ffilter_var\u51fd\u6570\u3002\n<\/p>\n

\n\t\u8bed\u6cd5
\nfilter_var(variable, filter, options)
\n\u53c2\u6570 \u63cf\u8ff0
\nvariable \u5fc5\u9700\u3002\u89c4\u5b9a\u8981\u8fc7\u6ee4\u7684\u53d8\u91cf\u3002
\nfilter \u53ef\u9009\u3002\u89c4\u5b9a\u8981\u4f7f\u7528\u7684\u8fc7\u6ee4\u5668\u7684 ID\u3002
\noptions \u89c4\u5b9a\u5305\u542b\u6807\u5fd7\/\u9009\u9879\u7684\u6570\u7ec4\u3002\u68c0\u67e5\u6bcf\u4e2a\u8fc7\u6ee4\u5668\u53ef\u80fd\u7684\u6807\u5fd7\u548c\u9009\u9879\u3002\n<\/p>\n

\n\tPHP Filters
\n<\/h2>\n
\n\n
\n\n\t\t\t\tID \u540d\u79f0\n\t\t\t<\/th>\n\n\t\t\t\t\u63cf\u8ff0\n\t\t\t<\/th>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_CALLBACK<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u8c03\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8fc7\u6ee4\u6570\u636e\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_STRING<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u53bb\u9664\u6807\u7b7e\uff0c\u53bb\u9664\u6216\u7f16\u7801\u7279\u6b8a\u5b57\u7b26\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_STRIPPED<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u201cstring\u201d \u8fc7\u6ee4\u5668\u7684\u522b\u540d\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_ENCODED<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\tURL-encode \u5b57\u7b26\u4e32\uff0c\u53bb\u9664\u6216\u7f16\u7801\u7279\u6b8a\u5b57\u7b26\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_SPECIAL_CHARS<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\tHTML \u8f6c\u4e49\u5b57\u7b26 \u2018\u201d<>& \u4ee5\u53ca ASCII \u503c\u5c0f\u4e8e 32 \u7684\u5b57\u7b26\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_EMAIL<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u5220\u9664\u6240\u6709\u5b57\u7b26\uff0c\u9664\u4e86\u5b57\u6bcd\u3001\u6570\u5b57\u4ee5\u53ca !#$%&\u2019*+-\/=?^_`{|}~@.[]\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_URL<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u5220\u9664\u6240\u6709\u5b57\u7b26\uff0c\u9664\u4e86\u5b57\u6bcd\u3001\u6570\u5b57\u4ee5\u53ca $-_.+!*\u2019(),{}|\\\\^~[]`<>#%\u201d;\/?:@&=\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_NUMBER_INT<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u5220\u9664\u6240\u6709\u5b57\u7b26\uff0c\u9664\u4e86\u6570\u5b57\u548c +-\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_NUMBER_FLOAT<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u5220\u9664\u6240\u6709\u5b57\u7b26\uff0c\u9664\u4e86\u6570\u5b57\u3001+- \u4ee5\u53ca .,eE\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_SANITIZE_MAGIC_QUOTES<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u5e94\u7528 addslashes()\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_UNSAFE_RAW<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u4e0d\u8fdb\u884c\u4efb\u4f55\u8fc7\u6ee4\uff0c\u53bb\u9664\u6216\u7f16\u7801\u7279\u6b8a\u5b57\u7b26\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_VALIDATE_INT<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u5728\u6307\u5b9a\u7684\u8303\u56f4\u4ee5\u6574\u6570\u9a8c\u8bc1\u503c\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_VALIDATE_BOOLEAN<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u5982\u679c\u662f \u201c1\u2033, \u201ctrue\u201d, \u201con\u201d \u4ee5\u53ca \u201cyes\u201d\uff0c\u5219\u8fd4\u56de true\uff0c\u5982\u679c\u662f \u201c0\u2033, \u201cfalse\u201d, \u201coff\u201d, \u201cno\u201d \u4ee5\u53ca \u201c\u201d\uff0c\u5219\u8fd4\u56de false\u3002\u5426\u5219\u8fd4\u56de NULL\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_VALIDATE_FLOAT<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u4ee5\u6d6e\u70b9\u6570\u9a8c\u8bc1\u503c\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_VALIDATE_REGEXP<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u6839\u636e regexp\uff0c\u517c\u5bb9 Perl \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9a8c\u8bc1\u503c\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_VALIDATE_URL<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u628a\u503c\u4f5c\u4e3a URL \u6765\u9a8c\u8bc1\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_VALIDATE_EMAIL<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u628a\u503c\u4f5c\u4e3a e-mail \u6765\u9a8c\u8bc1\u3002\n\t\t\t<\/td>\n<\/tr>\n
\n
\n\t\t\t\tFILTER_VALIDATE_IP<\/span>\n\t\t\t<\/td>\n
\n\t\t\t\t\u628a\u503c\u4f5c\u4e3a IP \u5730\u5740\u6765\u9a8c\u8bc1\u3002\n\t\t\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\n\tExample #1 A filter_var()<\/span> example\n<\/p>\n

\n
\n
\n\n
\n
\n
\n\t\t\t\t\t\t\t1\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t2\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t3\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t4\n\t\t\t\t\t\t<\/div>\n<\/td>\n
\n
\n
\n\t\t\t\t\t\t\t\t<?php\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\tvar_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\tvar_dump(filter_var('http:\/\/example.com<\/a>', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t?>\n\t\t\t\t\t\t\t<\/div>\n<\/p><\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<\/div>\n

\n\t\u4ee5\u4e0a\u4f8b\u7a0b\u4f1a\u8f93\u51fa\uff1a\n<\/p>\n

\n
\n
string(15) \"bob@example.com\"\r\nbool(false)<\/pre>\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

\u4ee5\u524d\u7528PHP\u9a8c\u8bc1\u90ae\u7bb1\u3001URL\u3001IP\u662f\u5426\u5408\u6cd5\u90fd\u662f\u901a\u8fc7\u81ea\u5df1\u5199\u6b63\u5219\u6765\u5b9e\u73b0\uff0c\u4f46\u662f\u6709\u65f6\u5019\u8111\u5b50\u53d1\u660f\uff0c\u53ef\u80fd\u4f1a\u5199\u51fa\u4e00\u4e2a\u4e0d\u662f\u5b8c\u5168 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/77\/revisions\/78"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}