如何使用PHP将基于两个字符串的文本拆分为一个分隔符(例如星号和换行符)



你能帮我根据"与">逻辑表达式,对于由六个星号和一个新行特殊字符组成的分隔符?

我是PHP的新手,正在实习,我必须解决一个遗留问题。

根据我的基本知识,这两个分隔符都是特殊字符,需要一个转义函数。

这是我的数据结构

第一个区块不以任何分隔符开头。
******
第二个区块与第一个区块由六个星号加一个回车分隔。
******
第三个区块与第二个区块由六个星号加一个回车分隔。
******
儿子继续…继续。
******
但我们可能会在段落中找到其他星号,可能有六个像******一样的星号,而这些星号是不可分割的。

没有******的新行也是如此,应该保持不变。


******
还要注意,在我的数据源中,块的数量是可变的。我知道这是一个结构糟糕的问题,但我们研究了存储组合块的好处,并选择它来节省存储空间和时间。我听说这样的模式很糟糕,但我们需要处理一些遗留问题。

这就是我想要的结果
区块1:第一个区块不以任何分隔符开头。

区块2:第二个区块与第一个区块由六个星号加一个回车分隔。

区块3:第三个区块与第二个区块由六个星号加一个回车分隔。

区块4:儿子继续…继续。

区块5:但我们可能会在段落中发现其他星号,可能有六个像******一样的星号,而这些星号是不可分割的。

没有******的新行也是如此,应该保持不变。


这是我在改编了互联网上的explodeEscaped函数后使用的代码

print_r(explodeEscaped('***', $my_text_variable, 'n'));


此功能已在此平台上进行了讨论,网址为[https://stackoverflow.com/questions/8519793/php-explode-but-ignore-escaped-delimiter][1] 。
这是我从代码中得到的结果


第一个区块不以任何分隔符开头。
1=>[2] =>第二个区块与第一个区块由六个星号加一个回车分隔。
[3]=>[4] =>第三个区块与第二个区块由六个星号加一个回车分隔
[5]=>[6] =>儿子继续…继续。
[7]=>[8] =>但我们可能会发现其他星号,可能有六个,比如[9]=>[10] =>在段落中,并且这些段落不可分割。

对于没有[11]=>[12] =>应该保持不变。


[13]=>还要注意,在我的数据源中,块的数量是可变的。我知道这是一个结构糟糕的问题,但我们研究了存储组合块的好处,并选择它来节省存储空间和时间。

class testit(){
public function __construct(){
$str = "this is a string ****** some text to be removed /r/n";
echo $this->delete_all_between("******", "/r/n", $str);
// returns this is a string some text to be removed
}
private function delete_all_between($beginning, $end, $string) {
$beginningPos = strpos($string, $beginning);
$endPos = strpos($string, $end);
if ($beginningPos === false || $endPos === false) {
return $string;
}
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
return $this->delete_all_between($beginning, $end, str_replace($textToDelete, '', $string)); // recursion to ensure all occurrences are replaced
}
}

我更改了算法,并成功地用以下代码替换了新行开头的分隔符:

$pattern = '/^[*]+/m';
preg_replace($pattern, '######', $text)); // Strip off the starting asterisks

这给了我一个不同的问题,我可以在哈希标签上进行拆分,因为我不必逃避它们。

要按完全由六个连续星号组成的行分割文本块,并修剪任何前导和尾随换行符,我建议使用以下模式,利用行首和行尾锚以及R元字符来匹配可能围绕分隔线的换行序列(nrn(。

代码:(演示(

$txt = <<<TXT
First chunk doesn't start with any delimiter.
******
Second chunk is separated from the first one by six asterisks plus a carriage return.
******
Third chunk is separated from the second one by six asterisks plus a carriage return.
******
And son on... and on.
******
But we might find other asterisks, maybe six like ****** inside the paragraph, and those one are not to be split.
Same for new lines without ****** which should stay the same.
TXT;
var_export(
preg_split('/R*^*{6}$R*/m', $txt, 0, PREG_SPLIT_NO_EMPTY)
);

输出:

array (
0 => 'First chunk doesn't start with any delimiter.',
1 => 'Second chunk is separated from the first one by six asterisks plus a carriage return.',
2 => 'Third chunk is separated from the second one by six asterisks plus a carriage return.',
3 => 'And son on... and on.',
4 => 'But we might find other asterisks, maybe six like ****** inside the paragraph, and those one are not to be split.
Same for new lines without ****** which should stay the same.',
)

, 0, PREG_SPLIT_NO_EMPTY参数对于所提供的输入字符串来说并不重要,但我发现包含这些标志可以使输出更加稳定/可靠。

相关内容

最新更新