如果您使用,则PREG_SPLIT_DELIM_CAPTURE
需要在与preg_split
.
在您当前的模式中:
/<img/
有东西可以捕捉,这就是为什么你看到它被移除(Demo):
Array
(
[0] => <p>First sentence here comes. Second sentence here it is. One more sentence. </p>
[1] => alt="amj" src="https://domain.com/images7.jpg" />
[2] => alt="Ea" src="http://domain.com/images3.jpg" />
[3] => alt="amj" src="https://domain.com/images7.jpg" />
[4] => alt="amj" src="https://domain.com/images7.jpg" />
)
但是,如果您从中创建捕获,它将被捕获:
/(<img)/
结果(演示):
Array
(
[0] => <p>First sentence here comes. Second sentence here it is. One more sentence. </p>
[1] => <img
[2] => alt="amj" src="https://domain.com/images7.jpg" />
[3] => <img
[4] => alt="Ea" src="http://domain.com/images3.jpg" />
[5] => <img
[6] => alt="amj" src="https://domain.com/images7.jpg" />
[7] => <img
[8] => alt="amj" src="https://domain.com/images7.jpg" />
)
如您所见,preg_split
它是否已记录在案,并且会在每次捕获第一个捕获 supgroup时添加另一个拆分(它只会占用第一个)。然后,您可能需要将其扩展到完整标签,例如,在其他类似 html-like-string-regex 的问题中已经概述了该标签(像往常一样受到正则表达式的限制,因此责怪您使用 preg_* 函数而不是 HTML如果遇到问题,解析器,而不是模式本身:
/(<img [^>]*>)/
结果(演示):
Array
(
[0] => <p>First sentence here comes. Second sentence here it is. One more sentence. </p>
[1] => <img alt="amj" src="https://domain.com/images7.jpg" />
[2] =>
[3] => <img alt="Ea" src="http://domain.com/images3.jpg" />
[4] =>
[5] => <img alt="amj" src="https://domain.com/images7.jpg" />
[6] =>
[7] => <img alt="amj" src="https://domain.com/images7.jpg" />
[8] =>
)
通过使用标准的 HTML 解析器,您可以使您的代码更加稳定。