如何在PHP中将文本块拆分为2个字符串?

时间:2022-02-17 20:14:34

I have a text-block, with this content:

我有一个文本块,包含以下内容:

[/+]There is my first text[+/]. There is my second text.

[/+]There is my third text.[+/]
There is my fourth text.

...

Now, I want to split it into 2 strings:

现在,我想将其拆分为2个字符串:

  1. $str_1 = "There is my first text.";
  2. $ str_1 =“我的第一个文字。”;

  3. $str_2 = " There is my second text.
  4. $ str_2 =“我的第二个文字。

[/+]There is my third text.[+/]
There is my fourth text.

...";

Could you show me: "How to split a text-block into 2 strings, in PHP?".

你能告诉我:“如何在PHP中将文本块拆分为2个字符串?”。

2 个解决方案

#1


1  

You can use explode function in php

你可以在php中使用explode功能

explode($delimiter, "String");

#2


1  

I'm not sure about your question but I think you might be looking for this

我不确定你的问题,但我认为你可能正在寻找这个问题

$str = "[/+]There is my first text[+/]. There is my second text.

[/+]There is my third text.[+/]
There is my fourth text.";

preg_match_all('/(\[\/\+])?([\w\s.]+)(\[\+\/])?/',$str,$m);

$str = "";
foreach($m[2] as $k => $v){
  ${"str".$k} = $v;
}

echo $str0."\n";
echo $str1."\n";
echo $str2."\n";
echo $str3."\n";

Demo

#1


1  

You can use explode function in php

你可以在php中使用explode功能

explode($delimiter, "String");

#2


1  

I'm not sure about your question but I think you might be looking for this

我不确定你的问题,但我认为你可能正在寻找这个问题

$str = "[/+]There is my first text[+/]. There is my second text.

[/+]There is my third text.[+/]
There is my fourth text.";

preg_match_all('/(\[\/\+])?([\w\s.]+)(\[\+\/])?/',$str,$m);

$str = "";
foreach($m[2] as $k => $v){
  ${"str".$k} = $v;
}

echo $str0."\n";
echo $str1."\n";
echo $str2."\n";
echo $str3."\n";

Demo