2015-11-21 57 views
1

我有一個文本塊,與此內容:如何在PHP中將文本塊拆分爲2個字符串?

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

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

... 

現在,我想把它分成2個字符串:

  1. $str_1 = "There is my first text.";
  2. $str_2 = " There is my second text.
[/+]There is my third text.[+/]
There is my fourth text. 

...";

你能告訴我:「如何在PHP中將文本塊分成2個字符串?」。

+0

至少在你問問題之前嘗試一些東西......新手不是藉口。 – Andrew

回答

1

可以使用爆炸功能在PHP

explode($delimiter, "String"); 
+1

[/ +],[+ /]。是分隔符,對嗎? – Ray

+0

有很多方法可以做到這一點, 1.爆炸-2倍或3倍 2. preg_split- $ output = preg_split('/(\ [\/\ \ \] | \ [\ + \/\])/ ',「yout string」); – Ray

1

我不知道你的問題,但我想你可能會找這個

$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

相關問題