Chapter 3 of zendframework Learning (Core Component-filter creation custom filter)

Source: Internet
Author: User

Many common filters have been defined in the zend_filter component. However, this does not meet our programming environment needs. For example, someone writes some statements in your system that are not pornographic, violent, or clean ", "Draft a master draft a sister" or something. It doesn't matter. zend_filter also supports custom filters ~! In this section, let's learn about custom filters.

To write a custom filter, you must define a class and reference the zend_filter_interface interface. This interface defines the filter () method, which can be implemented in your class. You can use the zend_filter: addfilter () method to add an object implementing this interface to the filter (this is a sentence in the book. It doesn't matter if you can understand it, it is estimated that the question of ZF version should be modified. The following example confirms that this book is out ~!).

Here is an example:

Public Function zdyjgaction ()
{


$ Temp1 = "Fuck your mom, fuck your uncle, fuck your sister !!! "; // No. If you see it, it hurts...
Echo $ temp1;

Echo "<p> ";
$ Filter = new application_model_myfilter (); // use a custom class to create an object.
Echo $ filter-> filter ($ temp1); // call the custom filtering method in the class.
}

This is a method in the Controller that calls a custom class. Create the myfilter. php file in your modles folder. The file content is:

<? PHP

// Require_once 'zend/filter/interface. PHP '; // it seems that I can skip this sentence if I use this version. I don't know whether you can write or not .....


Class application_model_myfilter implements zend_filter_interface
{

Public Function Filter ($ value)
{
$ Badlist = array ("Fuck your mom", "Fuck your sister", "fuck your uncle"); // hey, filter them out, you can also write your own rules.

For ($ I = 0; $ I <count ($ badlist); $ I ++)
{
$ Value = str_replace ($ badlist [$ I], "*", $ value); // filter specified content, change $ badlist [$ I] in $ value to "*".

}
Return $ value;

}

}

You can also use the addfilter () method of zend_filter to add a custom filter to the filter chain.

Puzzled:

1. during the test, when I first created a class, the write class-class myfilter implements zend_filter_interface is not recognized. It's a tough nut to crack. Later, a buddy told me how to write the class application_model_myfilter implements zend_filter_interface, that is, the class name cannot be written directly .. As for why, he said that there was no multi-module configuration, and he would not have configured multiple modules! Next I will look for some relevant knowledge to study what configuration modules to share with you, or find some good articles ..

2. It may be a version issue. The previously called method should be written as follows:

Public Function zdyjgaction ()
{$ Filter = new zend_filter ();
$ Filter-> addfilter (New myfilter ());
$ Filter-> filter ($ temp1 );

Now, you can write it like this:

$ Filter = new application_model_myfilter (); // use a custom class to create an object.
Echo $ filter-> filter ($ temp1); // call the custom filtering method in the class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.