This article describes the method of implementing custom filters by Zend Framework. Share it for your reference, as follows:
Create a custom filter
Code:
<?php require_once 'Zend/Filter/'; class MyFilter implements Zend_Filter_Interface{ public function filter($value){ $badlist = array("pear","strawberry","apple"); for($i = 0;$i<count($badlist);$i++){ $value = str_replace($badlist[$i], "*", $value); } return $value; } } $filter = new MyFilter(); $temp1 = "Oranges are delicious"; $temp2 = "I like eating apples"; $temp3 = "Pears are not bad, but my favorite thing to eat is strawberries!"; echo "content:".$temp1."<p>Filtered as:"; echo $filter->filter($temp1); echo "<p>"; echo "content:".$temp2."<p>Filtered as:"; echo $filter->filter($temp2); echo "<p>"; echo "content:".$temp3."<p>Filtered as:"; echo $filter->filter($temp3); echo "<p>";
result:
Content: Oranges are delicious
After filtering, it is: Oranges are delicious
Content: I like eating apples
After filtering, I like to eat*
Content: Pears are not bad, but my favorite thing to eat is strawberries!
After filtering, it is: * is not bad, but my favorite thing to eat is *!
analyze:
After inheriting the interface, implement the filter method. Then instantiate this class and call this method. You can complete the content you want to filter.
For more information about Zend, please visit the special topic of this site:Zend FrameWork Framework Introduction Tutorial》、《Summary of excellent development framework for php》、《Yii framework introduction and common techniques summary》、《ThinkPHP Introduction Tutorial》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Zend Framework framework.