How would I use PHP to extract everything in between [startstring]
and [endstring]
from this string:
我如何使用PHP从此字符串中提取[startstring]和[endstring]之间的所有内容:
[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.
我想找到[startstring]和[endstring]的所有匹配项,并回显里面的文本。有点像分界符。而且我希望所有的比赛都被空间分开。
How would I go about accomplishing this? Would this require regex
? Could you provide a sample?
我将如何完成这项工作?这需要正则表达式吗?你能提供样品吗?
Thanks so much! :)
非常感谢! :)
2 个解决方案
#1
5
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
3
Try with preg_match_all
:
尝试使用preg_match_all:
preg_match_all('/\[startstring\](.*?)\[endstring\]/', $input, $matches);
var_dump($matches);
#1
5
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
3
Try with preg_match_all
:
尝试使用preg_match_all:
preg_match_all('/\[startstring\](.*?)\[endstring\]/', $input, $matches);
var_dump($matches);