输入字符串:
:txt{sometext}:alpha
我想提取这样的数据(从括号中提取):
结果使用preg_match_all()
:
sometext
尝试这样,但这一切都不起作用:
php > preg_match_all('/^(\:txt)(.*)+(\{)(.*)+(\})/i', ':txt{sometext}:alpha', $m); var_dump($m);
array(6) {
[0] =>
array(1) {
[0] =>
string(14) ":txt{sometext}"
}
[1] =>
array(1) {
[0] =>
string(1) ":"
}
[2] =>
array(1) {
[0] =>
string(0) ""
}
[3] =>
array(1) {
[0] =>
string(1) "{"
}
[4] =>
array(1) {
[0] =>
string(0) ""
}
[5] =>
array(1) {
[0] =>
string(1) "}"
}
}
注意:作为示例我喜欢这个:txt{sometext}:alpha:another{mydata}
,因此我可以从:another
中提取数据并提供mydata
之类的结果。
结果:
Sniffer的结果:
php > preg_match_all('/(?<=:txt{)([^}]+)(?=})/', ':txt{sometext}:alpha', $x); var_dump($x);
array(2) {
[0] =>
array(1) {
[0] =>
string(8) "sometext"
}
[1] =>
array(1) {
[0] =>
string(8) "sometext"
}
}
Jerry的结果:
php > preg_match_all('/^:txt\{([^}]+)\}/', ':txt{sometext}:alpha', $x); var_dump($x);
array(2) {
[0] =>
array(1) {
[0] =>
string(14) ":txt{sometext}"
}
[1] =>
array(1) {
[0] =>
string(8) "sometext"
}
}