PHP函数删除字符串之间的字符除非标记匹配特定的字符串

时间:2017-04-28 06:23:06

标签: php arrays regex string

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

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

2)开始令牌字符串参数

3)停止令牌字符串参数

4)不要删除字符串参数

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

安装程序将是这样的:

这是设置功能的方式:

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

我认为这是我想要的最接近的东西,但我看不出如何将这个想法扩展到我的应用程序。任何帮助都会受到重视!

2 个答案:

答案 0 :(得分:0)

希望这会对您有帮助,我在这里使用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;
}

答案 1 :(得分:0)

我的建议是使用let keyArr = this.customPropertiesArray.map(function (item: any) { return item.name }); let isDuplicate = keyArr.some(function (item: any, idx: any) { return keyArr.indexOf(item) != idx }); if (isDuplicate) { console.log("Duplicate found"); }

let array=[
{"name":"name1","value":"value1"},
{"name":"name2","value":"value2"},
{"name":"name1","value":"value42"},
{"name":"name2","value":"value52"},
{"name":"name3","value":"value2"},
{"name":"name2","value":"value2"}...];