<? PHP
/***** Note:
* When the required parameters are passed to the filter function, the filter () function first
* Use preg_match_all () to filter all tags in the string ()
* Obtain it, And then loop through the match array of preg_match_all, through preg_split ()
* The function splits each label into the form of "Left attribute" = "right value", and
* Loop in the left attribute array. The preg_split () Matching content is retrieved to form
* Value that can be replaced, and then the corresponding tag in the string is replaced by str_replcae ()
* Function list:
* Function filter (& $ STR, $ tag, $ keep_attribute)
* Function match ($ Reg, & $ STR, $ ARR)
* Function show ($ STR, $ Title = '', $ DEBUG = true)
* Example:
* // Obtain the homepage of Sohu news
* $ STR = @ file_get_content ("http: // news. Sohu.com ");
* // Filter
* Filter ($ STR, 'A', 'href, target, alt ');
* Filter ($ STR, 'P', 'align ');
* Show ($ STR, 'filtered content ');
**************************************** *********/
$ Start_time = array_sum (explode ("", microtime ()));
$ STR =
<A style = "A" target = _ blank href = 'HTTP: // www. A.com 'xxx = xadsfa alt = "A a" style = "AA"> Site A </a>
<A alt = 'B B B' xxx = xadsfa target = _ blank href = 'HTTP: // www. B .com 'style = "B" style = "BB"> Site B </a>
<A xxx = xadsfa style = "C" href = 'HTTP: // www. C.com 'target = _ blank alt = c style = "cc"> Site C </a>
<A style = "D" href = 'HTTP: // www. D.com 'xxx = xadsfa alt = d target = _ blank style = "DD"> site D </a>
<A target = _ blank style = "E" xxx = xadsfa style = "ee" alt = e href = 'HTTP: // www. E.com '> site e </a>
<P align = right style = "font-size: 10px"> adasdfasdf </P>
<P style = "font-color: red;" align = 'left'> asdfasdfasdfasdf </P>
<P align = left right center> asdfasdfasdf </P>
<Font color = "red" alt = adasd adsasd> asdfadsfasdf </font>
<Font align = 'left' color = Red> asdfasdfadf </font>
<Font align = left right color = Red Black> ASDFASDF </font>
HTML;
// Display the original string
Show ($ STR, 'html ');
/***/
// Filter
Filter ($ STR, 'A', 'href, target, alt ');
Filter ($ STR, 'P', 'align ');
Filter ($ STR, 'font', 'color, alt ');
// Display the filtered content
Show ($ STR, 'result ');
// Script running time
$ Run_time = array_sum (explode ("", microtime ()-$ start_time;
Echo ('<center> script Run Time:'. $ run_time. '</center> ');
/**
* Description: filters HTML strings.
* Parameters:
* $ STR: the HTML string to be filtered
* $ Tag: the type of the filtered tag.
* $ Keep_attribute:
* Attributes to be retained. The parameter format is
* Href
* Href, target, ALT
* Array ('href ', 'target', 'alt ')
*/
Function Filter (& $ STR, $ tag, $ keep_attribute ){
// Check the parameter transfer method of the attribute to be retained
If (! Is_array ($ keep_attribute )){
// When the array is not passed in, determine whether the parameter contains, no.
If (strpos ($ keep_attribute ,',')){
// Contains, number, splitting parameter string as an array
$ Keep_attribute = explode (',', $ keep_attribute );
} Else {
// A pure string to construct an array
$ Keep_attribute = array ($ keep_attribute );
}
}
Echo ("· filter [$ tag] tags and keep attributes:". implode (',', $ keep_attribute). '<br> ');
// Obtain all tags to be processed
$ Pattern = "/<$ tag (. *) <\/$ tag>/I ";
Preg_match_all ($ pattern, $ STR, $ out );
// Process each tag cyclically
Foreach ($ out [1] as $ key => $ Val ){
// Several =
$ CNT = preg_split ('/* =/I', $ Val );
$ CNT = count ($ CNT)-1;
// Construct a regular expression
$ Pattern = '';
For ($ I = 1; $ I <= $ CNT; $ I ++ ){
$ Pattern. = '(. * = .*)';
}
// Complete regular expression formation, such as/(<A) (. * =. *) (. * =. *) (>. * <\/A>/I style
$ Pattern = "/(<$ tag) $ pattern (>. * <\/$ tag>)/I ";
// Get reserved attributes
$ Replacement = match ($ pattern, $ out [0] [$ key], $ keep_attribute );
// Replace
$ STR = str_replace ($ out [0] [$ key], $ replacement, $ Str );
}
}
/**
* Description: Creates tags and retains the attributes to be retained.
* Parameter: $ REG: Expression of pattern and preg_match
* $ STR: String, HTML string
* $ Arr: array, the attribute to be retained
* Return value:
* Return the retained tag, as shown in figure
* <A href = 'HTTP: // www. E.com 'target = _ blank alt = e> E. Com </a>
*/
Function match ($ Reg, & $ STR, $ ARR ){
// Match
Preg_match ($ Reg, $ STR, $ out );
// Retrieve the reserved attributes
$ Keep_attribute = '';
Foreach ($ arr as $ k1 => $ V1 ){
// Defines the array of attributes to be retained
Foreach ($ out as $ k2 => $ V2 ){
// Matching = Array
$ Attribute = trim (substr ($ V2, 0, strpos ($ V2, '= ')));
// = Previous
If ($ V1 = $ attribute ){
// The attributes to be retained are the same as those before = of the matched values.
$ Keep_attribute. = $ V2;
// Save the value of this matching part
}
}
}
// Construct the return value. The structure is as follows: <a href = xxx target = xxx class = "XXX"> aadd </a>
$ Keep_attribute = $ out [1]. $ keep_attribute. ($ out [count ($ out)-1]);
// Return Value
Return $ keep_attribute;
}
/**
* Display string content
*/
Function show ($ STR, $ Title = '', $ DEBUG = true ){
If ($ Debug ){
If (is_array ($ Str )){
$ STR = print_r ($ STR, true );
}
$ Txtrows = count (explode ("\ n", $ Str) + 1;
Echo ($ title. ': <br> <textarea name = "TXT" rows = "'. $ txtrows. '"Cols =" 130 "type =" parmname "text =" parmname "> '. $ Str. '</textarea> <br> ');
}
}
?>