Common PHP functions: filter HTML strings

Source: Internet
Author: User

<? Php
/*************************************** *****************************
* Process description:
* 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 = <HTML
<A style = "a" target = _ blank href = http://www.bkjia.com xxx = xadsfa alt = "a" style = "aa"> site a </a>
<A alt = 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.php100.net 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, retain 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 ("", $ str) + 1;
Echo ($ title.: <br> <textarea name = "txt" ROWS = ". $ txtRows. "COLS =" 130 ">. $ str. </TEXTAREA> <br> );
}

}

?>

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.