简体   繁体   English

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

[英]split a string at first occurrence of html tag

How can I split a string into 2 variables at first occurrence of a specific html tag (section in this case)?如何在第一次出现特定 html 标记(在本例中为部分)时将字符串拆分为 2 个变量?

$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';

Use preg_split使用preg_split

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

Third paramter 2 specifies the no of items would be returned after splitting.第三个参数2指定拆分后将返回的项目数。 So this would give you only two parts.所以这只会给你两个部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM