PHP在<;上拆分或分解字符串;img>;标签
我想把标签上的字符串分成不同的部分PHP在<;上拆分或分解字符串;img>;标签,php,regex,split,html-parsing,explode,Php,Regex,Split,Html Parsing,Explode,我想把标签上的字符串分成不同的部分 $string = 'Text <img src="hello.png" /> other text.'; $string='Text-other-Text'; 下一个函数还没有按正确的方式工作 $array = preg_split('/<img .*>/i', $string); $array=preg_split('/'), 3=>“其他文本。” ) 我应该用什么样的模式来完成它 编辑 如果有多个标签呢 array(
$string = 'Text <img src="hello.png" /> other text.';
$string='Text-other-Text';
下一个函数还没有按正确的方式工作
$array = preg_split('/<img .*>/i', $string);
$array=preg_split('/'),
3=>“其他文本。”
)
我应该用什么样的模式来完成它
编辑
如果有多个标签呢
array(
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => ' other text.'
)
$string='Text hello other Text';
$array=preg_split('/(',,
3=>“你好”,
4 => '',
5=>“其他文本。”
)
您的路径是正确的。您必须以以下方式设置标志:
$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
$array=preg_split(“/(您好其他文本)”;
$array=preg_split('/(“
[2]=>
字符串(7)“你好”
[3]=>
字符串(21)”
[4]=>
字符串(12)“其他文本”
}
您还需要在模式中包含所述的非贪婪字符(?),以强制它抓取第一个出现的实例。“/(hello other text.”;
$array=preg_split('/(“
[2] =>
字符串(7)“你好”
[3] =>
字符串(21)”
[4] =>
字符串(12)“其他文本”
}
这已经过时了吗?当我尝试回显这段代码时,我只看到:“array”@twan,你是如何使用它的?我已经修复了它,使用的是echo而不是print\r($array)lol。
array (
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => 'hello ',
4 => '<img src="bye.png" />',
5 => ' other text.'
)
$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img[^>]+\>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
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."
}
$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);
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."
}