如何分割文本块成2个字符串,与PHP


How to split a text-block into 2 strings, with PHP?

我有一个文本块,内容如下:

There is my first text.
---
There is my second text, 1.
There is my second text, 2.
...

现在,我想把它分成两个字符串:

  1. $str_1 = "There is my first text.";
  2. $str_2 = "There is my second text, 1. There is my second text, 2.";

我如何将文本块分割成2个字符串与PHP?


:

1. ---可以比较多,如:---------
2. There is my first text.总是文本块的开头。


因为,我的文本结构被改变了。所以,我的问题有一些小改动。我很抱歉。

爆炸是你的朋友。使用[*]作为分隔符对文本块进行爆破,并在其上使用array_filter来删除第一个空元素。

$str = "[*]There is my first text.[*]
There is my second text, 1.
There is my second text, 2.";
$result = array_filter(explode("[*]", $str));

执行后,第一个文本将在$result[0],第二个文本将在$result[1]

编辑:当你改变了你的需求,使用preg_split:

$str = "There is my first text.
---
There is my second text, 1.
There is my second text, 2.";
print_r(preg_split("/-+/", $str));

这将使用任意数量的-'s作为分隔符来分隔文本

参见php explosion

像这样

$arr = explode("'n", $str)

将生成一个包含以下内容的数组

[0] = [*]There is my first text
[1] = 
[2] = There is my second text, 1
[3] = There is my second text, 2