简体   繁体   English

如何preg_match()字符串并将所有结果输出到数组中?

[英]How to preg_match() a string and output all results into an array?

I want to output all matching results into an array. 我想将所有匹配的结果输出到数组中。 I'm only able to output one word and not both. 我只能输出一个字,而不能两个都输出。

$input = "I like to eat <cookies> with <coke>.";
$output = [];

preg_match('~<(.*?)>~', $input, $output);
echo $output[1];

It should result with something like: 结果应为:

$output = ["cookies", "coke"];

You were really close. 你真的很亲近 Use preg_match_all() instead. 请改用preg_match_all()

$input = "I like to eat <cookies> with <coke>.";
$output = [];

preg_match_all('~<(.*?)>~', $input, $output);
print_r($output[1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM