尝试确保 html 标签没有在 php 中保持打开状态

发布于 2024-11-05 12:16:19 字数 930 浏览 0 评论 0原文

我无法弄清楚为什么这段代码不起作用:

<?php
  $text = "<a><li><ul><ol>Hello";
  $tags = array('a', 'li', 'ul', 'ol');
  $tagcount = count($tags);
  $i = 0;

  while ($i < $tagcount) {
      $opentag = "<".$tags[$i];
      $closetag = "</".$tags[$i].">";

      if (stripos($text, $opentag)) {
          $lastopen = strripos($text, $opentag);
          $lastclose = strripos($text, $closetag);

          if ($lastopen > $lastclose) {
              $text = substr($text, 0, $lastopen);
              echo $tags[$i] . " tag was open. ";
          } else {
              echo $tags[$i] . " tag was closed. ";
      } else {
          echo $tags[$i] . " tag was not open. ";
      $i++;
  }
?>

它应该做的至少表明 $tags 数组中的所有标签都已打开。它的目的是使用 substr() 来确保没有标签打开,但它不起作用。运行这个给出:

标签未打开。 li 标签已打开。 ul 标签未打开。 ol 标签未打开。

即使它们都是开放的。任何帮助将不胜感激。

I cannot figure out why this code is not working:

<?php
  $text = "<a><li><ul><ol>Hello";
  $tags = array('a', 'li', 'ul', 'ol');
  $tagcount = count($tags);
  $i = 0;

  while ($i < $tagcount) {
      $opentag = "<".$tags[$i];
      $closetag = "</".$tags[$i].">";

      if (stripos($text, $opentag)) {
          $lastopen = strripos($text, $opentag);
          $lastclose = strripos($text, $closetag);

          if ($lastopen > $lastclose) {
              $text = substr($text, 0, $lastopen);
              echo $tags[$i] . " tag was open. ";
          } else {
              echo $tags[$i] . " tag was closed. ";
      } else {
          echo $tags[$i] . " tag was not open. ";
      $i++;
  }
?>

What it should do is at least signify that all the tags in the $tags array are open. It's meant to use substr() to make sure none of the tags are open but it's not working. Running this gives:

a tag was not open. li tag was open. ul tag was not open. ol tag was not open.

even though they are all open. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风流物 2024-11-12 12:16:19

看来你的逻辑有缺陷:如果没有找到针,strripos返回false,因此在你的内部if语句中你正在测试一个数字是否是大于false

对于外部 if 语句,您需要测试 false:

if (stripos($text, $opentag) !== false) {
  // found at position 0 or more...

您的内部 if 应该类似于:

    if (($lastclose !== false) && ($lastopen > $lastclose)) {

It seems your logic is flawed: strripos returns false if the needle is not found so in your inner if statement you are testing if a number is greater than false.

For your outer if statement, you need to test for false:

if (stripos($text, $opentag) !== false) {
  // found at position 0 or more...

Your inner if should be something like:

    if (($lastclose !== false) && ($lastopen > $lastclose)) {
恬淡成诗 2024-11-12 12:16:19

是“未打开”,因为 stripos 将返回第一次出现的位置,并且第一次出现位于索引 0(计算结果为 false)。

  • 被发现是打开的,因为它的索引不为零。但随后您截断搜索字符串,以便在零索引处找到下一次迭代
      ...
  • 将 if 更改为 stripos($text, $opentag) === false 并查看是否允许您找到处于打开状态的 a 标记。您必须弄清楚如何处理 substr(...) 因为我认为您的业务逻辑很可能会决定这一点。

    <a> is 'not open' because stripos will return the position of the first occurrence and the first occurrence is at index 0 (which evaluates to false).

    <li> is found to be open because its index is not zero. But then you truncate the search string so that the next iteration <ul> is found at zero index...

    Change your if to stripos($text, $opentag) === false and see if that allows you to find the a tag as being open. You will have to figure out what to do about the substr(...) since I think your business logic will most likely dictate that.

    忆悲凉 2024-11-12 12:16:19

    下面是一个使用正则表达式的示例:

      $text = "<a><li><ul><ol>Hello";
      $tags = array('a', 'li', 'ul', 'ol');
      $tagcount = count($tags);
      $i = 0;
      $matches = array();
      foreach ($tags as $tag)
      {
        $closed = preg_match_all("/<\/".$tag.">/i", $text, $matches);
        $open = preg_match_all("/<".$tag.">/i", $text, $matches);
    
        if ($open == 0)
        {
            echo $tag." was not opened, ";
        }
        else if ($open > $closed)
        {
            echo $tag." was left open, ";
        }
        else
        {
            echo $tag." was closed properly, ";
        }
    }
    

    Here's an example that works using regular expressions:

      $text = "<a><li><ul><ol>Hello";
      $tags = array('a', 'li', 'ul', 'ol');
      $tagcount = count($tags);
      $i = 0;
      $matches = array();
      foreach ($tags as $tag)
      {
        $closed = preg_match_all("/<\/".$tag.">/i", $text, $matches);
        $open = preg_match_all("/<".$tag.">/i", $text, $matches);
    
        if ($open == 0)
        {
            echo $tag." was not opened, ";
        }
        else if ($open > $closed)
        {
            echo $tag." was left open, ";
        }
        else
        {
            echo $tag." was closed properly, ";
        }
    }
    
    凉风有信 2024-11-12 12:16:19

    解析 HTML 并不简单,有一些很好的库可以为您完成这项工作。 Tidy 库自 PHP 5 起可用,可用于解析和整齐的 HTML 片段,或完整的页面输出。有一篇关于 devzone 的很好的 文章 展示了如何使用它,包括如何将其与 输出缓冲

    对于您发布的代码,您不应该在这样的 if 语句中使用 strpos 。引用PHP手册:

    警告:此函数可能返回布尔值 FALSE,但也可能返回非布尔值其计算结果为 FALSE,例如 0 或 "" 。 。 。使用 === 运算符来测试该函数的返回值。

    因此,要测试是否在字符串中找不到子字符串,请执行以下操作:

    if(strpos($haystack, $needle) === FALSE)
    

    并测试是否找到了子字符串:

    if(strpos($haystack, $needle) !== FALSE)
    

    但我真的、真的建议使用预先存在的库进行 HTML 操作或验证,尤其是在安全敏感的情况下(例如,反 XSS)。

    Parsing HTML is non-trivial, and there are some good libraries out there to do the work for you. The Tidy library has been available since PHP 5, and can be used to parse and tidy HTML fragments, or complete page output. There's a good article on devzone which shows how to use it, including how to combine it with output buffering.

    With respect to the code you've posted, you shouldn't use strpos in an if statement like that. To quote the PHP manual:

    Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "" . . . Use the === operator for testing the return value of this function.

    So to test that a substring was not found in a string, do:

    if(strpos($haystack, $needle) === FALSE)
    

    And to test that a substring was found:

    if(strpos($haystack, $needle) !== FALSE)
    

    But I really, really would advise using a pre-existing library for HTML manipulation or validation, especially if it is security-sensitive (anti-XSS, for example).

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文