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