【发布时间】:2018-01-02 11:24:35
【问题描述】:
我有三个字。但我只需要{}中的那些词 喜欢:
text1 {text2} text3
仅获取 { } 中的文本
Result = text2
And
result = text1 text3 (remove {} word )
and
from text1 {text2} text3 {text4}
result = text2 and text4
【问题讨论】:
标签: php
我有三个字。但我只需要{}中的那些词 喜欢:
text1 {text2} text3
仅获取 { } 中的文本
Result = text2
And
result = text1 text3 (remove {} word )
and
from text1 {text2} text3 {text4}
result = text2 and text4
【问题讨论】:
标签: php
为此尝试preg_match_all:
$a = 'text1 {text2} text3';
preg_match_all("/\\{(.*?)\\}/", $a, $matches);
print_R($matches[1][0]);
输出将是:
text2
【讨论】:
result = text1 text3 (remove {} word ) 和来自 text1 {text2} text3 {text4} result = text2 and text4
$matches 数组以获取$a = 'text1 {text2} {text3}'; 它将返回'数组([0] => 数组([0] => {text2} [1] => {text3}) [1] => 数组 ([0] => text2 [1] => text3) )'
一种解决方案是遍历每个字符。 如果字符是 { 然后开始将字符收集到 word 变量中。 如果你找到 } 字符,然后完成 - 你有一个单词。
【讨论】:
使用正则表达式:
$search = 'text1 {text2} text3';
preg_match('#\{(.*)\}#', $search, $match);
echo $match[1];
见preg_match文档
【讨论】: