Fox-FilterExamples

Back to Fox Categories: Fox

Below are examples of Fox custom filters that might prove useful.

Smileys

While you can eliminate the use of directives in Fox (indeed it is eliminated by default) by using $EnablePostDirectives = false; using smileys (as in :) or (:, in particular) will still break the page. You can avoid this by replacing these with special character codes.

  $FoxFilterFunctions['smiley'] = 'FoxSmileyFilter';

  function FoxSmileyFilter($pagename, $fields) {
    $fields = preg_replace('/\(:/', '(:', $fields);
    $fields = preg_replace('/:\)/', ':)', $fields);
    return $fields;
  }

URLs

When you have a Website field, you never know when people will type http:// or not. This filter will check and see, then add it if necessary.

  $FoxFilterFunctions['FoxFixUrl'] = 'FoxFixUrlFilter';

  function FoxFixUrlFilter($pagename, $fields) {
    if ($fields['website']!="") $matches = strpos("http://",$fields['website']);
    if ($matches==false && $fields['website'] != "") {
      $fields['website'] = "http://".$fields['website'];
    }
    return $fields;
  }
I don't know why, but the above doesn't work. It adds http:// even if it's already there. Until I figure out why (or someone who's not a PHP n00b explains that to me) I've added the following line above the "return $fields" line.

$fields['website'] = preg_replace('/http:\/\/http:\/\//', 'http:\/\/', $fields['website']);

E-mail

E-mails provide several potential pitfalls for a form submission. Here are a couple of examples of ways you can filter an e-mail field. First, if you just want to make sure something gets entered in the field, regardless of whether it's an e-mail or not, you can use this function (note you could use this kind of filter for any field that's necessary but not required):

  $FoxFilterFunctions['FoxVerifyEmail'] = 'FoxVerifyEmailFilter';

  function FoxVerifyEmailFilter($pagename, $fields) {
    if ($fields['email']=="") $fields['email']="noemail";
    return $fields;
  }

You may also want to validate that what was entered in the field was really an e-mail address and not something else. the following function will verify the most common email addresses:

  $FoxFilterFunctions['FoxVerifyEmail'] = 'FoxVerifyEmailFilter';

  function FoxVerifyEmailFilter($pagename, $fields) {
  if(!preg_match("/^[a-z0-9!#$%&'*+\^_`=\/{|}~-]+(?:\.[a-z0-9!#$%&'*+\^_`=\/{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2,4}|museum|travel)\b$/i", $fields['email'])) {
       $fields['email'] = "";
       FoxAbort($pagename, "Please provide a valid email address!");
    }
    return $fields;
  }

Note that email addresses can contain quoted strings, but the regex function above will not catch these.
See more about regex patterns for e-mail: