Home > Backend Development > PHP Tutorial > PHP implements completion of closed HTML tags_php tips

PHP implements completion of closed HTML tags_php tips

WBOY
Release: 2016-05-16 19:55:57
Original
1413 people have browsed it

The example in this article describes the completion of closed HTML tags in PHP. 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;
}

Copy after login

Test results:

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

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operating office document skills (including word, excel, access, ppt)", "php date Time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation Introductory tutorial " and " Summary of common PHP database operation skills "

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template