I have a text-block, with this content:
There is my first text.
---
There is my second text, 1.
There is my second text, 2.
...
Now, I want to split it into 2 strings:
$str_1 = "There is my first text.";
$str_2 = "There is my second text, 1. There is my second text, 2.";
How can I split a text-block into 2 strings with PHP?
Note:
1.
2.
1.
---
can be more, such as: ----
, -----
...2.
There is my first text.
is always the beginning of text-block.
Because, my text-structure was changed. So, my question has some mini changes. I am appoligized for this.
Answer:
Explode is your friend here. Explode the text-block with
[*]
as separator and use array_filter on it to remove the first empty element.$str = "[*]There is my first text.[*]
There is my second text, 1.
There is my second text, 2.";
$result = array_filter(explode("[*]", $str));
after execution you first text will be in
$result[0]
and second text in $result[1]
EDIT: after you changed your requirements, use 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));
This will separate the text using any number of
-'s
as a separatorAnswer:
See php explode
Something like so
$arr = explode($str, "\n")
Will generate an array with the following
[0] = [*]There is my first text
[1] =
[2] = There is my second text, 1
[3] = There is my second text, 2
No comments:
Post a Comment