使用php从以开头和结尾的字符串中获取所有匹配项 [英] Get all matches from string that start with and end, using php

查看:68
本文介绍了使用php从以开头和结尾的字符串中获取所有匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用 PHP 从这个字符串中提取 [startstring][endstring] 之间的所有内容:

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

我想找到[startstring][endstring]的所有匹配项,并回显里面的文本.有点像分隔符.我希望所有回显的匹配项都用空格分隔.

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.

我将如何实现这一目标?这需要 regex 吗?你能提供样品吗?

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

非常感谢!:)

推荐答案

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;
}

这篇关于使用php从以开头和结尾的字符串中获取所有匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆