首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP函数删除字符串中标记之间的字符,除非标记与特定字符串匹配

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

Stack Overflow用户
提问于 2017-04-28 14:23:06
回答 2查看 165关注 0票数 0

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

1)字符串(包含记号,记号之间有文本)

2)开始令牌字符串参数

3)停止令牌字符串参数

4) A请勿删除字符串参数

我希望将(1)长字符串传递给函数,并让它删除(2)开始标记的所有多个实例和(3)停止标记之前的所有文本,除非(4)Do NOT DELETE Parameter出现在字符串的该部分中。

设置如下所示:

下面是该函数的设置方法:

代码语言:javascript
复制
function CleanUpMyString($string-1, $start-2, $end-3, $keep-4){
// Working Code That Does The Work Here
}

我可以传递给函数的字符串可能如下所示:

代码语言:javascript
复制
$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!>>>";

假设我像这样调用函数:

代码语言:javascript
复制
echo CleanUpMyString($stringTEST, '<<<', '>>>', 'keep>');

我将得到以下输出:

代码语言:javascript
复制
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

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

EN

回答 2

Stack Overflow用户

发布于 2017-04-28 14:59:40

希望这会对你有所帮助,这里我使用preg_matchstartend收集所有匹配的令牌,然后我们在matches上迭代以保留字符串的必需部分并删除un-necessary part

代码语言:javascript
复制
<?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;
}
票数 0
EN

Stack Overflow用户

发布于 2017-04-28 15:07:09

我的建议是使用preg_replace_callback

代码语言:javascript
复制
<?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>');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43673338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档