Get all matches from string that start with and end, using php

烈酒焚心 提交于 2019-12-09 02:51:44

问题


How would I use PHP to extract everything in between [startstring] and [endstring] from this string:

[startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] welcome to the universe

I'd like to find all the matches of [startstring] and [endstring], and echo the text inside. Kinda like a delimiter. And I would like all the matches echoed to be seperated by a space.

How would I go about accomplishing this? Would this require regex? Could you provide a sample?

Thanks so much! :)


回答1:


preg_match_all('/\[startstring\](.*?)\[endstring\]/s', $input, $matches);
echo implode(' ', $matches);

preg_match_all('/\[startstring\](.*?)\=(.*?)\[endstring\]/s', $input, $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
    $key = $matches[1][$i];
    $value = $matches[2][$i];
    $$key = $value;
}



回答2:


Try with preg_match_all:

preg_match_all('/\[startstring\](.*?)\[endstring\]/', $input, $matches);
var_dump($matches);


来源:https://stackoverflow.com/questions/14308154/get-all-matches-from-string-that-start-with-and-end-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!