好的,这就是我想要做的,我需要一个php函数,我可以传递4个参数。
1)字符串(包含记号,记号之间有文本)
2)开始令牌字符串参数
3)停止令牌字符串参数
4) A请勿删除字符串参数
我希望将(1)长字符串传递给函数,并让它删除(2)开始标记的所有多个实例和(3)停止标记之前的所有文本,除非(4)Do NOT DELETE Parameter出现在字符串的该部分中。
设置如下所示:
下面是该函数的设置方法:
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
我认为这是最接近我想要的东西,但我不知道如何将这个想法扩展到我的应用程序中。任何帮助都将不胜感激!
发布于 2017-04-28 14:59:40
希望这会对你有所帮助,这里我使用preg_match从start到end收集所有匹配的令牌,然后我们在matches上迭代以保留字符串的必需部分并删除un-necessary part。
<?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;
}发布于 2017-04-28 15:07:09
我的建议是使用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>');https://stackoverflow.com/questions/43673338
复制相似问题