无法在大字符串上使用 preg_replace

Wont able to use preg_replace on a large string

我正在尝试删除 <note> 标签中的一些词。我有一个很长的字符串

string(4687) "~~PB~~ {{:en:iot-open:remotelab:logotyp_1_.png?200|}} <note>testtest</note> ====== RoofTop Thermo Laboratory - intelligent house and heating management ====== The laboratory is located at nowhere, xxx, xxxxx on the roof of bu...... => 转储结果

问题是它不会从 note 标签

之间删除这个 testtest 字符串

我正在尝试使用我在 strip_tags 手册中找到的这个功能。

      function strip_tags_content($text, $tags = '', $invert = FALSE) {

  preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
  $tags = array_unique($tags[1]);

  if(is_array($tags) AND count($tags) > 0) {
    if($invert == FALSE) {
      return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</>@si', '', $text);
    }
    else {
      return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</>@si', '', $text);
    }
  }
  elseif($invert == FALSE) {
    return preg_replace('@<(\w+)\b.*?>.*?</>@si', '', $text);
  }
  return $text;
}

这是我的完整代码

foreach ($data as $line)
        {
            // Find list tag
            $posi = strpos($line, "* ");

            // No list ?
            if ($posi === false) {
                continue;
            }

            // Check indent
            if (($posi % 2) != 0){
                //echo "<li>Invalid indentation in TOC</li>\n";
            }

            // Calculate indent
            $indent = ($posi - 2) / 2;
            // Search for header
            $posh = strpos($line, "]]");

            // No header ?
            if ($posh === false) {
                continue;
            }
            // Extract file path
            $page_path = substr($line, $posi + 4, $posh - $posi - 4);
            $file_path = str_replace(":", "/", $page_path);
            $file_path = $this->getConf("homelab_datapages_folder").$file_path.".txt";
      $indent2 = 0;


            // Page file exists ?
            if (file_exists($file_path))
            {
                // Open file
                $page_content = htmlspecialchars(file_get_contents($file_path));
        $page_content = $this->strip_tags_content($page_content,'note',TRUE);
        $page_cont = strip_tags(html_entity_decode($page_content));
                // Shorten header
                $book_content .= $this->shorten_header($page_content, $indent, $indent2)."\n";

        var_dump($book_content);
        //$book_content .=
      }
            else
            {
                $book_content .= "---\n MISSING PAGE ---\n";
            }

            // Display page
            //echo "    <li>".$page_path." (".$indent.")</li>\n";
        }

可能是什么问题?

是我的字符串太长无法使用 preg_replase 还是我这里有误?

当你打电话时

$this->strip_tags_content($page_content,'note',TRUE);

preg_match_all 结果是一个空数组 $tags,因此之后的所有测试都是错误的,return 值始终是 $text,没有任何修改。

调用函数:

$this->strip_tags_content($page_content,'<note>',TRUE);
//                                       ^____^

我让它工作了。

问题出在 htmlspecialchars() 函数上。

$page_content = htmlspecialchars(file_get_contents($file_path));

$page_content = file_get_contents($file_path);