我想将标签上的字符串拆分成不同的部分.
$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)