Generally, use the strip_tags function of PHP to remove all html tags, remove spaces, and use substr or the cn_substr function implemented by yourself to intercept html tags. If you do not remove the html Tag first, there will be no closed tags for the string directly captured, and sometimes it will even be intercepted on the tag, such
Copy codeThe Code is as follows: </di...
Today, I encountered a page flip problem: the body is written in the rich text editor, and there is a page button in the editor. After clicking it, I will insert a blue one to the current cursor position.
Copy codeThe Code is as follows: Horizontal line. Then php directly saves the data to the database. When displayed, use the explode function
Copy codeThe Code is as follows: Tags are divided into an array, and a segment is displayed based on the current page number. However, there is a serious problem, such as writing in the Rich Text Editor:
Copy codeThe Code is as follows: <div style = "text-align: center">
Content of page 1
<Hr/>
Page 2 content
</Div>
If the explode function is used for separation,
The content on the first page is
Copy codeThe Code is as follows: <div style = "text-align: center">
Content of page 1
The content on the second page is:
Copy codeThe Code is as follows: page 2 content
</Div>
In this way, no closed labels are generated. Directly displaying them on the page will damage the page layout...
After thinking for a long time, I also found many online closetag functions. However, we found that the tags on the first page are not closed. There is no way for the second type of labels without a start.
The closetags method for the unclosed tags on the first page is:Copy codeThe Code is as follows: function closetags ($ html ){
// No need to complete the tag
$ Arr_single_tags = array ('meta', 'img ', 'br', 'link', 'region ');
// Match the start tag
Preg_match_all ('# <([a-z] + )(? :.*)? (? <! [/|/])> # IU ', $ html, $ result );
$ Openedtags = $ result [1];
// Tag close matching
Preg_match_all ('# </([a-z] +)> # iU', $ html, $ result );
$ Closedtags = $ result [1];
// Calculate the number of disabled tags. If the number is the same, html data is returned.
$ Len_opened = count ($ openedtags );
If (count ($ closedtags) ==$ len_opened ){
Return $ html;
}
// Sort the array and put the last opened tag at the beginning
$ Openedtags = array_reverse ($ openedtags );
// Traverse the Enable tag Array
For ($ I = 0; $ I <$ len_opened; $ I ++ ){
// If you need to complete the tags
If (! In_array ($ openedtags [$ I], $ arr_single_tags )){
// If the tag is not in the disabled tag
If (! In_array ($ openedtags [$ I], $ closedtags )){
// Directly complete the closed tag
$ 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 interpretation engine to help complete problematic html fragments. The procedure is as follows:
Copy codeThe Code is as follows: <script>
Var div = document. createElement ('div ');
Div. innerHTML = '<? Php echo ("<div> here is the intercepted html clip");?> ';
Document. write (div. innerHTML );
</Script>
The principle is to first write html fragments into an empty div and then read them from the div. Although the write and read attributes are both innerHTML, the written content is different from the obtained content. If incomplete html fragments are written, the browser will automatically complete the correction. It is a complete html dom segment.
However, this has a drawback. Because JavaScript loads content information, it will not optimize the search engine well.