Home >
Backend Development >
PHP Tutorial >
PHP repairs the implementation code of HTML tags that are not closed normally (supports nesting and nearby closing)_PHP tutorial
PHP repairs the implementation code of HTML tags that are not closed normally (supports nesting and nearby closing)_PHP tutorial
WBOY
Release: 2016-07-21 15:18:02
Original
987 people have browsed it
fixHtmlTag version 0.2 This version solves the problems left over from the last time, namely the nearby closing and nested closing problems. Please see the comments of the code for details.
" * 2. Nearest closing mode, CLOSE. This mode will modify the code in the form of "
Hello
Why is there no * closed?" to "
Hello
Why is it not closed
" * * In the nested closed mode (default, no special parameters are required), you can Pass in the * tag name that needs to be closed nearby. In this way, something like "
Hello
Me, too" will be converted into * "
Hello
Hello
". * When passing parameters, the index needs to be written as follows. Settings that do not need to be modified can be omitted * * $param = array( * 'html' => '', //required Fill in * 'options' => array( * 'tagArray' => array(); * 'type' => 'NEST', * 'length' => ; null, * 'lowerTag' => TRUE, * 'XHtmlFix' => TRUE, * ) * ); * fixHtmlTag($param); * * The meaning of the value corresponding to the above index is as follows * string $html The html code that needs to be modified * array $tagArray When in nested mode, the nearest closed tag array is required * string $type mode name, currently supports NEST and CLOSE modes. If set to CLOSE, the setting of the parameter $tagArray will be ignored, and all tags will be closed nearby * ini $length If you want to truncate a certain length, you can here Assignment, this length refers to the string length * bool $lowerTag Whether to convert all tags in the code to lowercase, the default is TRUE * bool $XHtmlFix Whether to handle tags that do not comply with the XHTML specification, about < br> Convert to
* * @author IT tumbler * @version 0.2 * @link http://yungbo.com IT tumbler * @link http://enenba.com/?post=19 XX * @param array $param array parameter, which needs to be assigned a specific index * @return string $result processed The following html code * @since 2012-04-14 */ function fixHtmlTag($param = array()) { //Default value of parameter $html = ' '; $tagArray = array(); $type = 'NEST'; $length = null; $lowerTag = TRUE; $XHtmlFix = TRUE;
//First get the one-dimensional array, that is, $html and $options (if parameters are provided) extract($param);
//If options exist, extract the relevant variables if (isset($options)) { extract($options); }
$result = ''; //The final html code to be returned $tagStack = array() ; //Tag stack, simulated with array_push() and array_pop() $contents = array(); //Used to store html tags $len = 0; //Initial length of string
//Set the closing tag $isClosed, the default is TRUE, if it needs to be closed nearby, its value will be false after successfully matching the start tag, and it will be true after successful closing $isClosed = true;
//Convert all tags to be processed to lowercase $tagArray = array_map('strtolower', $tagArray);
//"Legal" single closed tag $singleTagArray = array( '''' ''< input', ');
//Verify matching pattern $type, the default is NEST pattern $type = strtoupper($type); if (!in_array($type, array('NEST', 'CLOSE'))) { $type = 'NEST'; }
//With a pair of < and > ; is the delimiter, put the original html tag and the string in the tag into the array $contents = preg_split("/(<[^>]+?>)/si", $html, - 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach ($contents as $tag) { if ('' == trim($tag)) { $result .= $tag; continue; }
//Match standard single closing tags, such as
if (preg_match("/<(w+)[^/>]* ?/>/si", $tag)) { $result .= $tag; continue; }
//Match the start tag, if it is a single tag, it will be output Stack else if (preg_match("/<(w+)[^/>]*?>/si", $tag, $match)) { //If the previous tag is not closed, And the previous label belongs to the nearest closed type // Then close it and pop the previous label
// If the label is not closed if (false === $isClosed) { //Nearby closing mode, close all tags directly nearby if ('CLOSE' == $type) { $result .= '' . end($tagStack) . '>'; array_pop($tagStack); } //Default nesting mode, the tag provided by the nearest closing parameter else { if (in_array(end($tagStack)), $tagArray )) { $result .= '' . end($tagStack) . '>'; array_pop($tagStack); } } }
//If the parameter $lowerTag is TRUE, convert the tag name to lowercase $matchLower = $lowerTag == TRUE ? strtolower($match[1]) : $match[1];
$tag = str_replace('<' . $match[1], '<' . $matchLower, $tag); //Start a new tag combination $result .= $tag; array_push($tagStack, $matchLower);
//If it belongs to the agreed single tag, close it and pop it out of the stack foreach ($singleTagArray as $singleTag) { if (stripos($tag, $singleTag) !== false) { if ($XHtmlFix == TRUE) { $tag = str_replace('>', ' />', $tag); } array_pop($tagStack); } }
//Nearby closed mode, the status changes to unclosed if ('CLOSE' == $type ) { $isClosed = false; } //Default nesting mode, if the tag is located in the provided $tagArray, the status is changed to unclosed else { if (in_array ($matchLower, $tagArray)) { $isClosed = false; } } unset($matchLower); }
//Match the closing tag and pop it out of the stack if appropriate else if (preg_match("/(w+)[^/>]*?>/si", $tag, $match)) {
//If the parameter $lowerTag is TRUE, convert the tag name to lowercase $matchLower = $lowerTag == TRUE ? strtolower($match[1]) : $match[ 1];
if (end($tagStack) == $matchLower) { $isClosed = true; //The match is completed and the tag is closed $tag = str_replace('' . $match[1], '' . $matchLower, $tag); $result .= $tag; array_pop($tagStack); } unset($matchLower ); }
//If there are still unclosed tags in the stack, connect them to $result while (!empty($tagStack)) { $result .= '' . array_pop($tagStack) . '>'; } return $result; }
http://www.bkjia.com/PHPjc/325600.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325600.htmlTechArticlefixHtmlTag version 0.2 This version solves the problems left over from the last time, namely the nearest closure and nested closure problems. Please see the comments of the code for details. Copy the code The code is as follows: ?php /** * fi...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn