在PHP中爆炸字符串

mic*_*urk 3 html php arrays string explode

我有以下一些PHP代码.

最后,我想存储<p>Michael is 26</p>$ STR1变量,<p>He likes green cars.</p><p>And white footballs</p><p>And red lollies</p><p>And yellow suns</p>$ STR2变量.

因此,第一段中的所有内容基本上都是$ str1,其后所有内容都是$ str2.

<?php
    $str = "<p>Michael is 26</p><p>He likes green cars.</p><p>And white footballs</p><p>And red lollies</p><p>And yellow suns</p>";
    $strArray = explode('</p>', $str);
    $str1 = $strArray[0] . '</p> ';
    echo $str1;
    // variable to export remainder of string (minus the $str1 value) ?
?>
Run Code Online (Sandbox Code Playgroud)

我该如何实现这一目标?

非常感谢任何指针.

Nea*_*eal 8

使用explode带分隔符:

 explode('</p>', $str, 2);
Run Code Online (Sandbox Code Playgroud)

这将返回:

array (
   0 => '<p>Michael is 26',
   1 => '<p>He likes green cars.</p><p>And white footballs</p><p>And red lollies</p><p>And yellow suns</p>' 
)
Run Code Online (Sandbox Code Playgroud)

那么你所要做的就是:

$strArray = explode('</p>', $str, 2);
$str1 = $strArray[0] . '</p> ';
$str2 = $strArray[1];
echo $str1.$str2; //will output the original string again
Run Code Online (Sandbox Code Playgroud)