Line. PHP is then stored directly into the database. When displayed, use the Explode function according to this
tag to divide into an array, and then display a fragment based on the current page number. But there's a serious problem, like rich-text editor writes:
Copy Code code as follows:
<div style= "Text-align:center" >
Content of Page 1
Page 2 Content
</div>
If separated by the explode function,
The contents of the first page are
Copy Code code as follows:
<div style= "Text-align:center" >
Content of Page 1
The second page reads:
Copy Code code as follows:
This results in the absence of a closed tag, directly displayed on the page will damage the layout of the page ...
Thought for a long time, but also found a lot of online closetag functions. But the discovery was all about the closure of the first page that had no closed label. There is no way for a second label that has no beginning.
The closed Closetags method for the first page that has no closed label is:
Copy Code code as follows:
function Closetags ($html) {
Labels that do not need to be fully filled
$arr _single_tags = Array (' meta ', ' img ', ' br ', ' link ', ' area ');
Match start tag
Preg_match_all (' #< ([a-z]+) (?:. *)? <! [/|/ ]) > #iU ', $html, $result);
$openedtags = $result [1];
Match off label
Preg_match_all (' #</([a-z]+) > #iU ', $html, $result);
$closedtags = $result [1];
Calculates the number of turned off tabs, and returns the HTML data if the same
$len _opened = count ($openedtags);
if (count ($closedtags) = = $len _opened) {
return $html;
}
Put the sorted array, put the last open tag on the front
$openedtags = Array_reverse ($openedtags);
Traversal Open Tag Array
for ($i = 0; $i < $len _opened; $i + +) {
If you need a full label
if (!in_array ($openedtags [$i], $arr _single_tags)) {
If this label is not in the Closed tab
if (!in_array ($openedtags [$i], $closedtags)) {
Direct complement full closure label
$html. = ' </'. $openedtags [$i]. ' > ';
} else {
Unset ($closedtags [Array_search ($openedtags [$i], $closedtags)]);
}
}
}
return $html;
}
Later I thought of a way to use the browser's own HTML interpreter engine to help fill the problematic HTML fragment. The following are specific practices:
Copy Code code as follows:
<script>
var div = document.createelement (' div ');
div.innerhtml = ' <?php echo ("<div> here is intercepted HTML fragment");? > ';
document.write (div.innerhtml);
</script>
The principle is to first write the HTML fragment into an empty div, and then read it from the Div. Although the write and read properties are innerHTML, the content written and received is not the same. Oh. If you write an incomplete HTML fragment, the browser automatically complements the correction. It is already a complete HTML DOM fragment when it is read.
But this has a disadvantage, because it is JS loading content information, will be bad for search engine optimization.