PHP preg_split 如果不在大括号内 [英] PHP preg_split if not inside curly brackets
问题描述
我正在使用 PHP 制作脚本语言解释器.我有这种脚本语言的代码:
I'm makin' a scripting language interpreter using PHP. I have this code in that scripting language:
write {Hello, World!} in either the color {blue} or {red} or {#00AA00} and in either the font {Arial Black} or {Monaco} where both the color and the font are determined randomly
(是的,很难相信,但这就是语法)
(Yes, it's hard to believe but that's the syntax)
我必须使用哪个正则表达式来拆分它(按空格拆分),但前提是不在大括号内.所以我想把上面的代码变成这个数组:
Which regex must I use to split this (split by spaces) but only if not inside the curly brackets. So I want to turn the above code into this array:
- 写
- 你好,世界!
- 在
- 要么
- 颜色
- 蓝色
- 或
- 红色
- 或
- #00AA00
- 和
- 在
- 要么
- 字体
- Arial Black
- 或
- 摩纳哥
- 哪里
- 两者
- 颜色
- 和
- 字体
- 是
- 确定
- 随机
(大括号内的字符串以粗体显示在上面)大括号内的字符串每个必须是一个元素.所以 {Hello, World!} 不能是:1. 你好2. 世界!
(The strings inside the curly brackets are show above in bold) The strings inside the curly brackets must be one element each. So {Hello, World!} cannot be: 1. Hello, 2. World!
我该怎么做?
提前致谢.
推荐答案
使用这样的东西怎么样:
what about using something like this :
$str = 'write {Hello, World!} in either the color {blue} or {red} or {#00AA00} and in either the font {Arial Black} or {Monaco} where both the color and the font are determined randomly';
$matches = array();
preg_match_all('#\{.*?\}|[^ ]+#', $str, $matches);
var_dump($matches[0]);
哪个会让你:
array
0 => string 'write' (length=5)
1 => string '{Hello, World!}' (length=15)
2 => string 'in' (length=2)
3 => string 'either' (length=6)
4 => string 'the' (length=3)
5 => string 'color' (length=5)
6 => string '{blue}' (length=6)
7 => string 'or' (length=2)
8 => string '{red}' (length=5)
9 => string 'or' (length=2)
10 => string '{#00AA00}' (length=9)
11 => string 'and' (length=3)
12 => string 'in' (length=2)
13 => string 'either' (length=6)
14 => string 'the' (length=3)
15 => string 'font' (length=4)
16 => string '{Arial Black}' (length=13)
17 => string 'or' (length=2)
18 => string '{Monaco}' (length=8)
19 => string 'where' (length=5)
20 => string 'both' (length=4)
21 => string 'the' (length=3)
22 => string 'color' (length=5)
23 => string 'and' (length=3)
24 => string 'the' (length=3)
25 => string 'font' (length=4)
26 => string 'are' (length=3)
27 => string 'determined' (length=10)
28 => string 'randomly' (length=8)
,你只需要迭代这些结果;以{开头并以}结尾的将是您的重要"词,其他将是其余的.
The, you just have to iterate over those results ; the ones starting by { and ending by } will be your "important" words, and the others will be the rest.
在评论后识别重要词的一种方法是这样的:
Edit after the comment : one way to identify the important words would be something like this :
foreach ($matches[0] as $word) {
$m = array();
if (preg_match('#^\{(.*)\}$#', $word, $m)) {
echo '<strong>' . htmlspecialchars($m[1]) . '</strong>';
} else {
echo htmlspecialchars($word);
}
echo '<br />';
}
或者,就像你说的,使用 strpos 和 strlen 也可以工作 ;-)
Or, like you said, working with strpos and strlen would work too ;-)
这篇关于PHP preg_split 如果不在大括号内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!