小编Mar*_*ing的帖子

PHP在<img>标签上拆分或爆炸字符串

我想将标签上的字符串拆分成不同的部分.

$string = 'Text <img src="hello.png" /> other text.';
Run Code Online (Sandbox Code Playgroud)

下一个功能还没有以正确的方式工作.

$array = preg_split('/<img .*>/i', $string);
Run Code Online (Sandbox Code Playgroud)

输出应该是

array(
    0 => 'Text ',
    1 => '<img src="hello.png" />',
    3 => ' other text.'
)
Run Code Online (Sandbox Code Playgroud)

我应该用什么样的模式来完成它?

编辑 如果有多个标签怎么办?

$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
Run Code Online (Sandbox Code Playgroud)

输出应该是:

array (
  0 => 'Text ',
  1 => '<img src="hello.png" />',
  3 => 'hello ',
  4 => '<img src="bye.png" />',
  5 => ' other text.' …
Run Code Online (Sandbox Code Playgroud)

php regex split explode html-parsing

6
推荐指数
1
解决办法
2261
查看次数

标签 统计

explode ×1

html-parsing ×1

php ×1

regex ×1

split ×1