PHP method to implement html tag completion and filtering of web content

墨辰丷
Release: 2023-03-27 16:46:02
Original
1783 people have browsed it

这篇文章主要介绍了PHP实现网页内容html标签补全和过滤的方法,结合实例形式分析了php常见的标签检查、补全、闭合、过滤等相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP实现网页内容html标签补全和过滤的方法。分享给大家供大家参考,具体如下:

如果你的网页内容的html标签显示不全,有些表格标签不完整而导致页面混乱,或者把你的内容之外的局部html页面给包含进去了,我们可以写个函数方法来补全html标签以及过滤掉无用的html标签.

php使HTML标签自动补全,闭合,过滤函数方法一:

代码:

function closetags($html) {
 preg_match_all(&#39;#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU&#39;, $html, $result);
 $openedtags = $result[1];
 preg_match_all(&#39;#</([a-z]+)>#iU&#39;, $html, $result);
 $closedtags = $result[1];
 $len_opened = count($openedtags);
 if (count($closedtags) == $len_opened) {
    return $html;
 }
 $openedtags = array_reverse($openedtags);
 for ($i=0; $i < $len_opened; $i++) {
    if (!in_array($openedtags[$i], $closedtags)) {
     $html .= &#39;</&#39;.$openedtags[$i].&#39;>&#39;;
    }else {
     unset($closedtags[array_search($openedtags[$i], $closedtags)]);
    }
 }
 return $html;
}
Copy after login

closetags()解析:

array_reverse() : 此函数将原数组中的元素顺序翻转,创建新的数组并返回。如果第二个参数指定为 true,则元素的键名保持不变,否则键名将丢失。

array_search() : array_search(value,array,strict),此函数与in_array()一样在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。 如果第三个参数strict被指定为 true,则只有在数据类型和值都一致时才返回相应元素的键名。

php使HTML标签自动补全,闭合,过滤函数方法二:

function checkhtml($html) {
  $html = stripslashes($html);
    preg_match_all("/\<([^\<]+)\>/is", $html, $ms);
    $searchs[] = &#39;<&#39;;
    $replaces[] = &#39;<&#39;;
    $searchs[] = &#39;>&#39;;
    $replaces[] = &#39;>&#39;;
    if($ms[1]) {
      $allowtags = &#39;img|font|p|table|tbody|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li&#39;;//允许的标签
      $ms[1] = array_unique($ms[1]);
      foreach ($ms[1] as $value) {
        $searchs[] = "<".$value.">";
        $value = shtmlspecialchars($value);
        $value = str_replace(array(&#39;\\&#39;,&#39;/*&#39;), array(&#39;.&#39;,&#39;/.&#39;), $value);
        $value = preg_replace(array("/(javascript|script|eval|behaviour|expression)/i", "/(\s+|"|&#39;)on/i"), array(&#39;.&#39;, &#39; .&#39;), $value);
        if(!preg_match("/^[\/|\s]?($allowtags)(\s+|$)/is", $value)) {
          $value = &#39;&#39;;
        }
        $replaces[] = empty($value)?&#39;&#39;:"<".str_replace(&#39;"&#39;, &#39;"&#39;, $value).">";
      }
    }
    $html = str_replace($searchs, $replaces, $html);
  return $html;
}
//取消HTML代码
function shtmlspecialchars($string) {
  if(is_array($string)) {
    foreach($string as $key => $val) {
      $string[$key] = shtmlspecialchars($val);
    }
  } else {
    $string = preg_replace(&#39;/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/&#39;, &#39;&\\1&#39;,
      str_replace(array(&#39;&&#39;, &#39;"&#39;, &#39;<&#39;, &#39;>&#39;), array(&#39;&&#39;, &#39;"&#39;, &#39;<&#39;, &#39;>&#39;), $string));
  }
  return $string;
}
Copy after login

checkhtml($html)解析:

stripslashes():函数删除由addslashes()函数添加的反斜杠。该函数用于清理从数据库或HTML表单中取回的数据。

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

PHP Wrapper在SAE上的应用方法_php技巧

PHP实现的多文件上传类及用法示例_php技巧

PHP字符串中插入子字符串方法总结[原创]_php技巧

The above is the detailed content of PHP method to implement html tag completion and filtering of web content. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!