PHP implementation HTML Tag automatic complement code _ other comprehensive

Source: Internet
Author: User
In general, first use the PHP strip_tags function to remove all HTML tags, and then remove the space, and then use SUBSTR or their own implementation of the CN_SUBSTR function to achieve interception. Because if you don't remove the HTML tags first, there will be no closed labels for the strings that are intercepted, sometimes even on the labels like
Copy Code code as follows:
</di ...

Today I have a content page clipping problem: The text is written with a rich text editor, there is a paging button on the editor, click on the current cursor position to insert a blue

Copy Code code as follows:

Line. PHP is then stored directly into the database. When displayed, use the Explode function according to this

Copy Code code as follows:

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:

Page 2 Content
</div>

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.

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.