PHP中花括号之间的多个单词
我有以下字符串:PHP中花括号之间的多个单词,php,regex,split,Php,Regex,Split,我有以下字符串: $string = "Hello from {me} to {you}"; 我想要的是一个在花括号之间有单词的数组(当然没有花括号) array(2) { [0]=> string(2) "me" [1]=> string(3) "you" } 我尝试了以下模式,但它只显示一个选定的单词(带括号) /\{([^}]+)\}/ 或 我不熟悉正则表达式 谢谢您需要使用preg\u match\u all中的第三个参数来获取数组中的匹配值 <?
$string = "Hello from {me} to {you}";
我想要的是一个在花括号之间有单词的数组(当然没有花括号)
array(2) {
[0]=>
string(2) "me"
[1]=>
string(3) "you"
}
我尝试了以下模式,但它只显示一个选定的单词(带括号)
/\{([^}]+)\}/
或
我不熟悉正则表达式
谢谢您需要使用
preg\u match\u all
中的第三个参数来获取数组中的匹配值
<?php
$string = "Hello from {me} to {you}";
preg_match_all('/\{([^}]+)\}/', $string, $matches);
var_dump($matches);
?>
为了得到干净的版本
echo $matches[1][yourKey];
阅读材料
您需要使用
preg\u match\u all
中的第三个参数来获取数组中的匹配值
<?php
$string = "Hello from {me} to {you}";
preg_match_all('/\{([^}]+)\}/', $string, $matches);
var_dump($matches);
?>
为了得到干净的版本
echo $matches[1][yourKey];
阅读材料
使用
preg\u match\u all
。在下面的代码中,$results
是您要查找的:
$raw_string = "Hello from {me} to {you}";
$pattern = "/{(.*?)}/"; //will match everything in { }
if(preg_match_all($pattern,$raw_string,$matches)):
$results = $matches[1];
else:
//no matches
endif;
使用
preg\u match\u all
。在下面的代码中,$results
是您要查找的:
$raw_string = "Hello from {me} to {you}";
$pattern = "/{(.*?)}/"; //will match everything in { }
if(preg_match_all($pattern,$raw_string,$matches)):
$results = $matches[1];
else:
//no matches
endif;
$string=“你好,从{me}到{you}”
preg_match_all('/{([^}]+)}/',$string,$matches)
print_r($matches[count($matches)-1]);$string=“Hello from{me}to{you}”
preg_match_all('/{([^}]+)}/',$string,$matches)
print_r($matches[count($matches)-1]);您是否尝试过
preg_match_all
?@Script47是的,生成了两个数组,结果很奇怪。不是我想要的结果。。此用户返回{
和}
也是:\n虽然解决了您明确的问题,但我敢打赌这正是您真正需要的。您是否尝试了preg\u match\u all
?@Script47是的,我尝试了,生成了两个数组,结果很奇怪。不是我想要的结果。。此用户返回{
和}
too:\在回答您明确的问题时,我敢打赌这正是您真正需要的。@FirstOne哈哈,都是1分钟前!在我发布答案时,它显示了“显示评论”通知。@FirstOne是的,我将其包括在下面,只是为了让OP感兴趣而显示整个输出。@FirstOne哈哈,都是1分钟前!它显示了“显示评论”当我发布答案时通知。@FirstOne是的,我在下面包括了它,只是为了让OP感兴趣而展示整个输出。