如何查找某些文本模式的所有实例并将它们添加到 PHP 中的数组中


How to find all instances of certain text pattern and add them to an array in PHP?

例如,我有以下文本:

<content>Foo</content>This is only a <b>test</b><content>Bar</content>Again, only a test.

如何创建一个包含两个条目的数组,FooBar

preg_match_all("/<content>(.+?)<'/content>/is", $string, $matches);

您的结果将被放入$matches数组中。

使用 $matches[0]$matches[1] 等访问它们。

祝你好运!

编辑:preg_match_all()将为您提供所有结果!感谢@Blake的小费。