Home >Backend Development >PHP Tutorial >PHP implements completion of closed HTML tags, php completes html tags_PHP tutorial

PHP implements completion of closed HTML tags, php completes html tags_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:13888browse

PHP implements completion of closed HTML tags, php completes html tags

This article describes the example of PHP implementation of completion of closed HTML tags. Share it with everyone for your reference, the details are as follows:

Many times, when we make excerpts of articles, if HTML content appears, there will be an HTML tag that does not end in the intercepted article. In this case, the problem of page style confusion will occur. What we need at this time is to add the missing end tags in batches. I saw a function that is easier to handle on the official website of www.php.net, 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]+)(&#63;: .*)&#63;(&#63;<![/|/ ])>#iU', $html, $result);
  $opentags = $result[1];
  // put all closed tags into an array
  preg_match_all('#</([a-z]+)>#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 .= '</'.$opentags[$i].'>';
   }
   else
   {
    unset($closetags[array_search($opentags[$i], $closetags)]);
   }
  }
  return $html;
}

Test results:

<&#63;php
$content = '<div><p><span>越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!';
echo CloseTags($content);
/*
返回的结果是:
<div><p><span>
越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!</span></p></div>
*/
&#63;>

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "php-oriented "Introduction Tutorial on Object Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • PHP implementation of html tag closure detection and repair method
  • PHP implementation of filtering various HTML tags
  • php intercepts html Methods for strings and automatic completion of html tags
  • php method for filtering html tags in form submissions
  • php summary of regular expressions for filtering HTML tags, attributes, etc.
  • php Sharing functions for intercepting strings containing html tags
  • php Determine whether the string contains html tags
  • php Get web page title and content function (excluding html tags)
  • HTML tag filtering skills in PHP
  • Examples of removing HTML tags in PHP
  • Three solutions to deleting HTMl tags in PHP
  • php makes HTML tags automatically complete closed function codes
  • PHP implements HTML tag automatic completion code
  • php regular code to filter html tags, spaces, and newlines (with instructions)
  • Clear HTML in strings under php Tag code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113720.htmlTechArticlePHP implements completion of closed HTML tags, php completes html tags. This article describes how PHP implements completion of closures. HTML tag. I share it with you for your reference, as follows: Many times, when...
Statement:
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