2008
12.08

Filter Functions

一些實用的 filter functions ,某些地方可以取代 regex 驗證

  1. filter_has_var — Checks if variable of specified type exists
  2. filter_id — Returns the filter ID belonging to a named filter
  3. filter_input_array — Gets external variables and optionally filters them
  4. filter_input — Gets a specific external variable by name and optionally filters it
  5. filter_list — Returns a list of all supported filters
  6. filter_var_array — Gets multiple variables and optionally filters them
  7. filter_var — Filters a variable with a specified filter
/* for an example: */

var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL));
var_dump(filter_var('example@example.com', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('example.com', FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED));

今天才知道這個東西,以後如果有機會要驗證表單的話,很多常用的欄位都不需自己再寫 regex 了(詳見官方文件)。

No Comment.

Add Your Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>