/********************************************************************
* Process Description:
* When the parameter passed into the filter function is required, the filter () function first
* Put all the tags in the string to be filtered $tag through Preg_match_all ()
* Take out, then loop preg_match_all the matching array, by Preg_split ()
* Function splits each label as "left attribute" = "Right value" form, then
* Leave the array of attributes in the loop, and Preg_split () matches the contents of the corresponding take out, constitute
* Can replace the value, after the most by Str_replcae () replace the corresponding label in the string
* List of functions:
* Function Filter (& $str, $tag, $keep _attribute)
* Function Match ($reg,& $str, $arr)
* Function Show ($str, $title =, $debug = True)
* Use Example:
*//Get Sohu News Home
* $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 = <<< HTML
Site A
Site B
Site C
Site D
Site E
Adasdfasdf
Asdfasdfasdfasdf
Asdfasdfasdf
Asdfadsfasdf
Asdfasdfadf
Asdfasdf
HTML;
Show Original string
Show ($str, Html);
/************************************************************************************************************** *********************************************************/
Filter
Filter ($STR, A,href,target,alt);
Filter ($STR, p,align);
Filter ($STR, Font,color,alt);
Show filtered Content
Show ($str, Result);
Script Run time
$run _time = array_sum (Explode ("", Microtime ()))-$start _time;
Echo Script Run time:. $run _time. );
/**
* Description: Filter HTML string
Parameters
* $STR: HTML string to filter
* $tag: Filter Label type
* $keep _attribute:
* Properties to keep, this parameter form can be
* href
* Href,target,alt
* Array (Href,target,alt)
*/
Function filter (& $str, $tag, $keep _attribute) {
To check how parameters are passed for properties to be persisted
if (!is_array ($keep _attribute)) {
Does not pass the array in to determine if the parameter contains, the number
if (Strpos ($keep _attribute,,)) {
Include, number, slice parameter string array
$keep _attribute = Explode (,, $keep _attribute);
}else {
Pure string, constructed array
$keep _attribute = Array ($keep _attribute);
}
}
Echo ("• Filter [$tag] label, preserve properties:". Implode (,, $keep _attribute).
);
Get all the tokens to be processed
$pattern = "/< $tag (. *) /I ";
Preg_match_all ($pattern, $str, $out);
Cycle through each tag
foreach ($out [1] as $key = + $val) {
Get a mark in a few =
$cnt = Preg_split (/*=/i, $val);
$cnt = count ($cnt)-1;
Construct a matching regular
$pattern =;
for ($i =1; $i <= $cnt; $i) {
$pattern. = (. *=.*);
}
Complete the regular expression form, such as/(. */i style
$pattern = "/(< $tag) $pattern (>.* )/I ";
Get reserved Property
$replacement = Match ($pattern, $out [0][$key], $keep _attribute);
Replace
$str = Str_replace ($out [0][$key], $replacement, $STR);
}
}
/**
* Description: Constructs the label, retains the property to retain
* Parameters: $reg: Pattern,preg_match expression
* $str: string,html string
* $arr: Array, properties to keep
Returns
* Return to the label after the reservation has been processed, such as
* e.com
*/
Function Match ($reg,& $str, $arr) {
Match
Preg_match ($reg, $str, $out);
Remove a reserved property
$keep _attribute =;
foreach ($arr as $k 1=> $v 1) {
Defines an array of properties to preserve
foreach ($out as $k 2=> $v 2) {
Array after match =
$attribute = Trim (substr ($v 2,0,strpos ($v 2,=));
= Front of
if ($v 1 = = $attribute) {
The same as the previous part of the property to retain and match the value
$keep _attribute. = $v 2;
Save the value of this matching part
}
}
}
Constructs the return value, structure such as: Aadd
$keep _attribute = $out [1]. $keep _attribute. ($out [Count ($out)-1]);
return value
Return $keep _attribute;
}
/**
* Display string Contents
*/
Function Show ($str, $title =, $debug = True) {
if ($debug) {
if (Is_array ($STR)) {
$str = Print_r ($str, True);
}
$txtRows = count (Explode ("", $str)) 1;
Echo ($title.:
<textarea name="txt" rows=".$txtRows." cols="130">. $str.</textarea>
);
}
}
?>
http://www.bkjia.com/PHPjc/486205.html www.bkjia.com true http://www.bkjia.com/PHPjc/486205.html techarticle ? PHP/******************************************************************** * Process Description: * When the parameters of the attached requirements are passed into the filter function, The filter () function First * takes the word string ...