拆分成可变的其他变量 [英] Split a variable into other variables

查看:147
本文介绍了拆分成可变的其他变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好真棒人在互联网上!我需要一些帮助 :)
我有一个PHP脚本RCON,这个脚本保存RCON给一个变量命名结果的结果,这是一个例子。

Hello awesome people on the internet! I need some help :) I have a php rcon script, this script saves the result of the rcon to a variable named results, this is an example.

结果显示= 2追踪目标(S)为lluiscab: - RCON:4(RCON) - 测试:5555(测试)

results = Showing 2 tracked objective(s) for lluiscab:- rcon: 4 (rcon)- test: 5555 (test)

我要像一个RCON变量设置为4,并测试5555。
我用爆炸等认为,我在网上找到,但我不能使它工作。是否有人知道该怎么办呢?

I want to set a variable like rcon to 4 and test to 5555. I used explode and other thinks that I found on the web, but I can't make it work. Does someone know how to do it?

编辑:这变量的变化,所以,有时我能有RCON,测试和硬币和某个唯一RCON

This variable changes, so, sometimes I can have rcon, test and coins and sometime only rcon

推荐答案

您可以使用常规的前pression这一点。

You can use a regular expression for this.

preg_match('/rcon:\s*(\d+).*test:\s*(\d+)/', $line, $match);
$rcon = $match[1];
$test = $match[2];

\\ D + 匹配的是数字序列,并把()围绕它使一个捕获组。 $匹配包含由常规的前pression匹配的输入线的部分, $匹配[N] 包含第N个捕获组。

\d+ matches a sequence of numbers, and putting () around it makes it a capture group. $match contains the parts of the input line that were matched by the regular expression, and $match[N] contains the Nth capture group.

如果您需要捕获任何看起来像字:号,您可以用 preg_match_all 和关联数组。

If you need to capture anything that looks like word: number, you can use preg_match_all and an associative array.

preg_match_all('/(\w+):\s*(\d+)/', $line, $matches, PREG_SET_ORDER);
$results = array();
foreach ($matches as $match) {
    $results[$match[1]] = intval($match[2]);
}

对于例如输入,这将创建

For the example input, this will create

$results = array(
    'rcon' => 4,
    'test' => 5555
);

演示

这篇关于拆分成可变的其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆