简体   繁体   中英

Is there an alternative to the “filter_var” function in php when using HHVM?

I have been recently playing around with HHVM. Went through a lot of trouble getting it to work on my computer. I know that not all PHP functions are available. As a test, I am writing a new website using it instead of using my current code. I ran into a problem when trying to use

filter_var($var,FILTER_SANITIZE_URL);

From the error.log file, it turns out that this function is undefined. Is the filter_var function not available for use in HHVM or am I just doing something wrong here. I like to keep things DRY, this would mean I have to do a lot more validation than I expected.

filter_var is now implemented in hhvm. Open github issues if you have any problems with it.

This function appears to not have been implemented on HHVM See http://comments.gmane.org/gmane.science.linguistics.wikipedia.technical/70038

An option if you want to rely on this functionality with the hopes that it will enter the fold is to polyfill it in (partial implementation to inspire the motivated).

if (!function_exists("filter_var")){
      // define the constants used by the function 
      define("FILTER_VALIDATE_EMAIL", "email");

      function filter_var(){
          $args = func_get_args();
          // $args[1] is the filter type (second parameter)
          switch ($args[1]){
               case FILTER_VALIDATE_EMAIL:
                   if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $args[0])?$args[0]:false;
                   break;
          }
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM