Fix code for HTML tags that are not closed normally (supports nesting and nearby closing)
WBOY
Release: 2016-07-25 09:05:16
Original
1190 people have browsed it
/**
* fixHtmlTag
*
* html标签修复函数,此函数可以修复未正确闭合的 HTML 标签
*
* 由于不确定性因素太多,暂时提供两种模式“嵌套闭合模式”和
* “就近闭合模式”,应该够用了。
*
* 这两种模式是我为了解释清楚此函数的实现而创造的两个名词,
* 只需明白什么意思就行。
* 1,嵌套闭合模式,NEST,为默认的闭合方式。即 "
你好"
* 这样的 html 代码会被修改为 "
你好
"
* 2. Nearby closing mode, CLOSE. This mode will modify the code in the form of "
Hello
Why is there no
* closed?" to "
Why is it not closed
"
*
* In the nested closing mode (default, no special parameters are required), you can pass in the
* that needs to be closed nearby Tag name, in this way, something like "
Hello
Me, too" will be converted into
* "
Hello< /p>
I also like the form of
".
* 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
* '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 shall be When nesting mode, the nearest closed tag array is required
* string $type mode name, currently supports two modes: NEST and CLOSE. If set to CLOSE, the setting of the parameter $tagArray will be ignored, and all tags will be closed at the nearest
* ini $length If you want to truncate a certain length, you can assign a value here. This length refers to the length of the string.
* bool $lowerTag Whether to convert all tags in the code to lowercase, the default is TRUE
* bool $XHtmlFix Whether to handle inconsistencies XHTML standard tags, that is, converting to
*
* @author IT卤场
* @version 0.2
* @link http://bbs.it -home.org 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 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 relevant variables
if (isset($options)) {
extract($options);
}< ;/p>
$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 the string
//Set the closing mark $isClosed, the default is TRUE, if you need to close it nearby , after successfully matching the start tag, its value is false, and after successful closing, it is true
$isClosed = true;
//Convert all tags to be processed to lowercase
$tagArray = array_map('strtolower ', $tagArray);
//"Legal" single closed tag
$singleTagArray = array(
'
'
'
'
'
'
'
);
//Verification matching pattern $type , the default is NEST mode
$type = strtoupper($type);
if (!in_array($type, array('NEST', 'CLOSE'))) {
$type = 'NEST';
} p>
//Using a pair of < and > as delimiters, put the original html tag and the string in the tag into an array
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