【问题标题】:PHP function Delete characters in string Between Tokens Unless Tokens Match a specific StringPHP函数删除标记之间字符串中的字符除非标记匹配特定字符串
【发布时间】:2017-04-28 06:23:05
【问题描述】:

好的,这就是我想要做的,我需要一个可以传递 4 个参数的 php 函数。

1) 一个字符串(包含标记和它们之间的文本)

2) 起始令牌字符串参数

3) 一个停止令牌字符串参数

4) 一个 DO NOT DELETE 字符串参数

我想将 (1) 长字符串传递给函数并让它删除 (2) 开始标记的所有多个实例和所有文本直到 (3) 停止标记,除非 (4) 不要DELETE 参数存在于字符串的那部分。

设置如下:

这里是设置函数的方式:

function CleanUpMyString($string-1, $start-2, $end-3, $keep-4){
// Working Code That Does The Work Here
}

我可以传递给函数的字符串可能类似于以下示例:

$stringTEST = "This is the intro of the string, <<<This Part Would Be Deleted>>> in the next snippet of the string, <<<we will delete another part>>> This is going pretty well. <<<keep>We do not delete this part because of the added token part 'keep>'.>>> We will add some more rambling text here. Then we will add another snippet. <<<We will also delete this one.>>><<<keep>But in the end it is all good!>>>";

假设我这样调用函数:

echo CleanUpMyString($stringTEST, '<<<', '>>>', 'keep>');

我会得到以下输出:

This is the intro of the string, in the next snippet of the string, This is going pretty well. We do not delete this part because of the added token part 'keep>'. We will add some more rambling text here. Then we will add another snippet. But in the end it is all good!

我无法控制输入字符串,因此标记可以以任意数量出现在任何位置,并且它们出现的顺序没有合理的顺序。

我真的不知道从哪里开始。我看了一下这个帖子:

PHP function to delete all between certain character(s) in string

我认为这是最接近我想要的东西,但我不知道如何将这个想法扩展到我的应用程序。任何帮助将不胜感激!

【问题讨论】:

  • 看起来预期输出中的最后一句应该包含keep&gt;。对?请检查this approach - 结果是否符合预期?它可以改进,我只是想清除预期的行为。
  • @BobC 希望我的帖子能帮到你..
  • 嘿@WiktorStribiżew 感谢您的帮助。不,不应包含/保留字符串部分“keep>”,除非它前面没有 $start-2 变量。所以在这种情况下,我有“我们不会删除这部分,因为添加了标记部分'keep>'”。它应该被包括在内,但在其他任何地方它只用于指示字符串的哪一部分应该保留,因此应该被删除。所以我有部分“但最后一切都很好!>>>”我们会在那里删除它。非常感谢

标签: php arrays regex string


【解决方案1】:

我的建议是使用preg_replace_callback

<?php

function CleanUpMyString($string, $start, $end, $keep)
{
    return preg_replace_callback(
        '~' . preg_quote($start, '~') . '.+?' . preg_quote($end, '~') . '~',
        function ($M) use ($start, $end, $keep) {
            if (strpos($M[0], $keep)) {
                return str_replace([$start, $end, $keep], '', $M[0]);
            } else {
                return '';
            }
        },
        $string
    );
}

$stringTEST = "This is the intro of the string, <<<This Part Would Be Deleted>>> in the next snippet of the string, <<<we will delete another part>>> This is going pretty well. <<<keep>We do not delete this part because of the added token part 'keep>'.>>> We will add some more rambling text here. Then we will add another snippet. <<<We will also delete this one.>>><<<keep>But in the end it is all good!>>>";

echo CleanUpMyString($stringTEST, '<<<', '>>>', 'keep>');

【讨论】:

    【解决方案2】:

    希望这会对您有所帮助,我在这里使用preg_match 收集从startend 的所有匹配标记,然后我们将迭代matches 以保留字符串的所需部分并删除un-necessary part .

    Try this code snippet here

    <?php
    
    ini_set('display_errors', 1);
    $stringTEST = "This is the intro of the string, <<<This Part Would Be Deleted>>> in the next snippet of the string, <<<we will delete another part>>> This is going pretty well. <<<keep>We do not delete this part because of the added token part 'keep>'.>>> We will add some more rambling text here. Then we will add another snippet. <<<We will also delete this one.>>><<<keep>But in the end it is all good!>>>";
    echo CleanUpMyString($stringTEST, "<<<", ">>>", "keep>");
    
    function CleanUpMyString($stringTEST, $start, $end, $keep)
    {
        $startQuotes=  preg_quote($start);
        $endQuotes=  preg_quote($end);
        preg_match_all("/$startQuotes.*?(?:$endQuotes)/", $stringTEST,$matches);
        foreach($matches[0] as $key => $value)
        {
            if(stristr($value, $start.$keep)!==false)
            {
                $stringTEST= substr_replace($stringTEST,"",strpos($stringTEST, $start.$keep),  strlen($start.$keep));;
                $stringTEST= substr_replace($stringTEST,"",strpos($stringTEST, $end),  strlen($end));
            }
            else
            {
                $stringTEST=  str_replace($value, "", $stringTEST);
            }
        }
        return $stringTEST;
    }
    

    【讨论】:

    • 您应该使用preg_quote() 转义$start$end
    • 嘿@SahilGulati 非常感谢您的帮助。查看您的代码并使用它让我了解需要发生的事情的过程。话虽如此,这种方法似乎存在一些问题。一些应该保留的文本,不是。 'keep>' 部分也不应该是最终字符串的一部分,除非它前面没有 $start 变量。此外,当我在脚本中针对包含换行符的数据库中的字符串运行此函数时,它似乎会中断执行。我现在正在努力解决这个问题,以使其充分发挥作用。还有什么建议吗?
    • @BobC 不知何故我误解了你的帖子,我正在更新我的帖子,你的预期结果。
    猜你喜欢
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 2015-01-14
    • 2010-09-19
    • 2017-09-06
    相关资源
    最近更新 更多