在第一次出现html标签时拆分一个字符串

时间:2015-09-04 09:17:47

标签: php html regex string split

如何在第一次出现特定html标记时将字符串拆分为2个变量(本例中为section)?

$str = 'some text, <b>some text</b> <section class="first">abc</sextion><section class="second">abc</section> some text';

$first = 'some text, <b>some text</b> ';
$second = '<section class="first">abc</sextion><section class="second">abc</section> some text';

1 个答案:

答案 0 :(得分:0)

使用preg_split

preg_split('~(?=<section\b)~', $str, 2)

第三个参数2指定分割后将返回的项目数。所以这只会给你两个部分。

相关问题