PHP implements completion of closed HTML tags, and php supplements html tags _ PHP Tutorial

Source: Internet
Author: User
PHP implements completion of closed HTML tags, and php supplements html tags. PHP implements completion of closed HTML tags. php supplements html tags. this example describes how PHP implements completion of closed HTML tags. For your reference, we often use PHP to complete closed HTML tags and php to complete html tags.

The example in this article describes how to complete and Disable HTML tags in PHP. We will share this with you for your reference. The details are as follows:

Most of the time, when we make an article to intercept the abstract, if there is HTML content, there will be HTML tags that are not completed in the intercepted article. In this case, the page style will be disordered. In this case, we need to add the missing end tags in batches. On the www.php.net official website, you can see a better processing function, as shown below:

function CloseTags($html){  // strip fraction of open or close tag from end (e.g. if we take first x characters, we might cut off a tag at the end!)  $html = preg_replace('/<[^>]*$/','',$html); // ending with fraction of open tag  // put open tags into an array  preg_match_all('#<([a-z]+)(?: .*)?(?
 #iU', $html, $result);  $opentags = $result[1];  // put all closed tags into an array  preg_match_all('#
 #iU', $html, $result);  $closetags = $result[1];  $len_opened = count($opentags);  // if all tags are closed, we can return  if (count($closetags) == $len_opened) {   return $html;  }  // close tags in reverse order that they were opened  $opentags = array_reverse($opentags);  // self closing tags  $sc = array('br','input','img','hr','meta','link');  // ,'frame','iframe','param','area','base','basefont','col'  // should not skip tags that can have content inside!  for ($i=0; $i < $len_opened; $i++)  {   $ot = strtolower($opentags[$i]);   if (!in_array($opentags[$i], $closetags) && !in_array($ot,$sc))   {    $html .= '
 ';   }   else   {    unset($closetags[array_search($opentags[$i], $closetags)]);   }  }  return $html;}

Test results:

<? Php $ content ='

If you are busy, do you want to give yourself a vacation? Do you still remember when the last exercise was? When traveling outdoors, you can enjoy a different travel experience: If you are at your freedom, you can see scenery everywhere! '; Echo CloseTags ($ content);/* the returned result is:

If you are busy, do you want to give yourself a vacation? Do you still remember when the last exercise was? When traveling outdoors, you can enjoy a different travel experience: If you are at your freedom, you can see scenery everywhere!

*/?>

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.