PHP在标签上分割或爆炸字符串

时间:2022-04-15 03:02:26

i would like to split a string on a tag into different parts.

我想把标签上的字符串分割成不同的部分。

$string = 'Text <img src="hello.png" /> other text.';

The next function doesnt work yet on the right way.

下一个函数还没有以正确的方式工作。

$array = preg_split('/<img .*>/i', $string);

The output should be

输出应该

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

What kind of pattern should i use to get it done?

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

EDIT What if there are multiple tags?

如果有多个标签怎么办?

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

And the output should be:

输出为:

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

2 个解决方案

#1


2  

You are in the right path. You have to set the flag PREG_SPLIT_DELIM_CAPTURE in this way:

你走对了路。您必须以这种方式设置标志PREG_SPLIT_DELIM_CAPTURE:

$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

With multiple tag edited properly the regex:

通过正确编辑多个标签,regex:

$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img[^>]+\>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

This will output:

这将输出:

array(5) {
  [0]=>
  string(5) "Text "
  [1]=>
  string(22) "<img src="hello.png" >"
  [2]=>
  string(7) " hello "
  [3]=>
  string(21) "<img src="bye.png" />"
  [4]=>
  string(12) " other text."
}

#2


1  

You need to include non-greedy character (?) as described here into your pattern as well, to force it to grab the first occurring instance. '/(<img .*?\/>)/i'

您还需要将这里描述的非贪婪字符(?)包含到您的模式中,以强制它获取第一个发生的实例。' /(< img。* ? \ / >)/我的

so your example code will be something like:

你的示例代码是这样的:

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

var_dump($array);

Which result to printing :

印刷的结果:

array(5) {
    [0] =>
    string(5) "Text "
    [1] =>
    string(23) "<img src="hello.png" />"
    [2] =>
    string(7) " hello "
    [3] =>
    string(21) "<img src="bye.png" />"
    [4] =>
    string(12) " other text."
}

#1


2  

You are in the right path. You have to set the flag PREG_SPLIT_DELIM_CAPTURE in this way:

你走对了路。您必须以这种方式设置标志PREG_SPLIT_DELIM_CAPTURE:

$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

With multiple tag edited properly the regex:

通过正确编辑多个标签,regex:

$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img[^>]+\>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

This will output:

这将输出:

array(5) {
  [0]=>
  string(5) "Text "
  [1]=>
  string(22) "<img src="hello.png" >"
  [2]=>
  string(7) " hello "
  [3]=>
  string(21) "<img src="bye.png" />"
  [4]=>
  string(12) " other text."
}

#2


1  

You need to include non-greedy character (?) as described here into your pattern as well, to force it to grab the first occurring instance. '/(<img .*?\/>)/i'

您还需要将这里描述的非贪婪字符(?)包含到您的模式中,以强制它获取第一个发生的实例。' /(< img。* ? \ / >)/我的

so your example code will be something like:

你的示例代码是这样的:

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

var_dump($array);

Which result to printing :

印刷的结果:

array(5) {
    [0] =>
    string(5) "Text "
    [1] =>
    string(23) "<img src="hello.png" />"
    [2] =>
    string(7) " hello "
    [3] =>
    string(21) "<img src="bye.png" />"
    [4] =>
    string(12) " other text."
}