Php 如何从字符串中除去所有HTML标记(backticks中包含的标记除外)?

Php 如何从字符串中除去所有HTML标记(backticks中包含的标记除外)?,php,Php,我正在使用PHP的strip\u tags()函数从字符串中剥离标记。例如: $text = strip_tags( $text ); 我的目标是剥离所有标签,除非标签恰好包含在backticks中。如果标签包含在backticks中,我不想去掉它们 我的第一个想法是尝试使用strip\u tags()的第二个参数。这将允许我指定不被删除的允许标记。例如,strip_标记($text,)。然而,这并不是我想要的 如何从字符串中除去所有HTML标记(恰好包含在背景标记中的标记除外) Ref:如果

我正在使用PHP的
strip\u tags()
函数从字符串中剥离标记。例如:

$text = strip_tags( $text );
我的目标是剥离所有标签,除非标签恰好包含在backticks中。如果标签包含在backticks中,我不想去掉它们

我的第一个想法是尝试使用
strip\u tags()
的第二个参数。这将允许我指定不被删除的允许标记。例如,
strip_标记($text,
。然而,这并不是我想要的

如何从字符串中除去所有HTML标记(恰好包含在背景标记中的标记除外)


Ref:

如果你的转义序列是固定的,你可以做一些比模糊处理简单得多的事情(这是Marty在他的评论中建议的,如果我完全诚实的话,这是我最喜欢的技术之一)。即使要使用模糊处理或preg_替换,也需要考虑转义的滴答声

相反,您可以执行以下操作:

$strippeddown = array();
$breakdown = explode('`', $text);
$j = 1;

foreach ($breakdown AS $i => $gather)
{
    if ($j > 1)
    {
        $j--;
        unset($breakdown["$i"]);
        continue;
    }

    $j = 1;
    while (strrpos($gather, '\\') === 0 AND isset($breakdown[$i + $j]))
    {
        $gather = $breakdown[$i + $j];
        $breakdown["$i"] .= '`' . $gather;
        $j++;
    }
}

$breakdown = array_values($breakdown);

foreach ($breakdown AS $i => $gather)
{
   if (!$i OR !($i % 2))
   {
      $strippeddown[] = strip_tags($gather);
   }
   else
   {
      $strippeddown[] = $gather;
   }
}

$text = implode('`', $strippeddown);
function strip($input)
{
    preg_match_all('/`([^`]+)`/', $input, $retain);

    for($i = 0; $i < count($retain[0]); $i++)
    {
        // Replace HTML wrapped in backticks with match index.
        $input = str_replace($retain[0][$i], "{{$i}}", $input);
    }

    // Strip tags.
    $input = strip_tags($input);

    for($i = 0; $i < count($retain[0]); $i++)
    {
        // Replace previous replacements with relevant data.
        $replace = $retain[1][$i];

        // Do some stuff with $replace here - maybe check that it's a tag
        // you're comfortable with else use htmlspecialchars(), etc.
        // ...

        $input = str_replace("{{$i}}", $replace, $input);
    }

    return $input;
}

用一个答案来支持我的评论,比如:

$strippeddown = array();
$breakdown = explode('`', $text);
$j = 1;

foreach ($breakdown AS $i => $gather)
{
    if ($j > 1)
    {
        $j--;
        unset($breakdown["$i"]);
        continue;
    }

    $j = 1;
    while (strrpos($gather, '\\') === 0 AND isset($breakdown[$i + $j]))
    {
        $gather = $breakdown[$i + $j];
        $breakdown["$i"] .= '`' . $gather;
        $j++;
    }
}

$breakdown = array_values($breakdown);

foreach ($breakdown AS $i => $gather)
{
   if (!$i OR !($i % 2))
   {
      $strippeddown[] = strip_tags($gather);
   }
   else
   {
      $strippeddown[] = $gather;
   }
}

$text = implode('`', $strippeddown);
function strip($input)
{
    preg_match_all('/`([^`]+)`/', $input, $retain);

    for($i = 0; $i < count($retain[0]); $i++)
    {
        // Replace HTML wrapped in backticks with match index.
        $input = str_replace($retain[0][$i], "{{$i}}", $input);
    }

    // Strip tags.
    $input = strip_tags($input);

    for($i = 0; $i < count($retain[0]); $i++)
    {
        // Replace previous replacements with relevant data.
        $replace = $retain[1][$i];

        // Do some stuff with $replace here - maybe check that it's a tag
        // you're comfortable with else use htmlspecialchars(), etc.
        // ...

        $input = str_replace("{{$i}}", $replace, $input);
    }

    return $input;
}
功能条($input)
{
preg_match_all('/`([^`]+)`/',$input,$retain);
对于($i=0;$i
通过测试:

echo strip("Hello <strong>there</strong>, what's `<em>`up`</em>`?");
// Output: Hello there, what's <em>up</em>?
echo-strip(“你好那里,有什么事吗?”);
//输出:你好,怎么了?

你可以做一些事情,比如,在背景标记之间匹配所有内容,用匹配索引替换它们的内容,用旧内容替换标记。我认为strip_标记不适合你。你可能需要做一个preg_替换。感谢这个解决方案,虽然我不能在这个场合使用它,因为我不确定是否会逃过backticks。我的
$text
变量来自前面的endOkay,我已经修改了上面的示例,以便在一些反勾号可能被转义时如何执行。请记住,此代码未经测试;我刚刚在上面键入了它。如果您要向最终用户显示用户输入,您可能还希望使用htmlspecialchars或类似的内容来避开未剥离的标记。谢谢,我将研究htmlspecialchars+来自meI的1没有想到使用
preg\u match\u all
-只是检查了一下,它看起来非常适合这种情况。感谢您提供完整的解决方案!不幸的是,这并没有忽略可能使用\或其他转义序列转义的倒勾。@TED Phillips如果需要,一个小的正则表达式更新可以解决这个问题:-)