根据值前面的标签将冒号分隔的字符串解析为变量

发布于 2024-09-25 05:08:22 字数 293 浏览 7 评论 0原文

我们正在尝试获取字符串的某些部分。

我们有字符串:

location:32:DaD+LoC:102AD:Ammount:294

我们希望将信息放在不同的字符串中。例如 $location=32$Dad+Loc=102AD

每个字符串的值各不相同,但它始终具有以下结构: location:{number}:DaD+LoC:{code}:Ammount:{number}

我们如何获取这些值?

We are trying to get certain parts of a string.

We have the string:

location:32:DaD+LoC:102AD:Ammount:294

We would like to put the information in different strings. For example $location=32 and $Dad+Loc=102AD

The values vary per string, but it will always have this construction:
location:{number}:DaD+LoC:{code}:Ammount:{number}

How do we get those values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

遗失的美好 2024-10-02 05:08:22

这会产生你想要的结果,但是例如 $dad+Loc 在 PHP 中是一个无效的变量名,因此它不会按照你想要的方式工作,最好使用数组或 stdClass 对象而不是单个变量。

  $string = "location:32:DaD+LoC:102AD:Ammount:294";
  $stringParts = explode(":",$string);
  $variableHolder = array();
  for($i = 0;$i <= count($stringParts);$i = $i+2){
      ${$stringParts[$i]} = $stringParts[$i+1];
  }

  var_dump($location,$DaD+LoC,$Ammount);

That would produce what you want, but for example $dad+Loc is an invalid variable name in PHP so it wont work the way you want it, better work with an array or an stdClass Object instead of single variables.

  $string = "location:32:DaD+LoC:102AD:Ammount:294";
  $stringParts = explode(":",$string);
  $variableHolder = array();
  for($i = 0;$i <= count($stringParts);$i = $i+2){
      ${$stringParts[$i]} = $stringParts[$i+1];
  }

  var_dump($location,$DaD+LoC,$Ammount);
爱你不解释 2024-10-02 05:08:22

简单的快进方法:

$string = "location:32:DaD+LoC:102AD:Ammount:294";

$arr = explode(":",$string);

$location= $arr[1];
$DaD_LoC= $arr[3];
$Ammount= $arr[5];

Easy fast forward approach:

$string = "location:32:DaD+LoC:102AD:Ammount:294";

$arr = explode(":",$string);

$location= $arr[1];
$DaD_LoC= $arr[3];
$Ammount= $arr[5];
梦回梦里 2024-10-02 05:08:22

$StringArray = 爆炸 ( ":" , $string)

$StringArray = explode ( ":" , $string)

黯然#的苍凉 2024-10-02 05:08:22

通过使用 preg_split 并将结果数组映射到关联数组。
像这样:

$str  = 'location:32:DaD+LoC:102AD:Ammount:294';
$list = preg_split('/:/', $str);

$result = array();
for ($i = 0; $i < sizeof($list); $i = $i+2) {
    $result[$array[$i]] = $array[$i+1];
};

print_r($result);

By using preg_split and mapping the resulting array into an associative one.
Like this:

$str  = 'location:32:DaD+LoC:102AD:Ammount:294';
$list = preg_split('/:/', $str);

$result = array();
for ($i = 0; $i < sizeof($list); $i = $i+2) {
    $result[$array[$i]] = $array[$i+1];
};

print_r($result);
一紙繁鸢 2024-10-02 05:08:22

似乎没有人能正确地做到这一点

$string = "location:32:DaD+LoC:102AD:Ammount:294";
list(,$location,, $dadloc,,$amount) = explode(':', $string);

it seems nobody can do it properly

$string = "location:32:DaD+LoC:102AD:Ammount:294";
list(,$location,, $dadloc,,$amount) = explode(':', $string);
指尖凝香 2024-10-02 05:08:22

php 函数 split 已被弃用,因此建议使用 preg_split 或explode 代替它。
在这种情况下非常有用的是函数 list():

list($location, $Dad_Loc, $ammount) = explode(':', $string);

编辑:
我的代码有一个错误:

list(,$location,, $Dad_Loc,, $ammount) = explode(':', $string);

the php function split is deprecated so instead of this it is recommended to use preg_split or explode.
very useful in this case is the function list():

list($location, $Dad_Loc, $ammount) = explode(':', $string);

EDIT:
my code has an error:

list(,$location,, $Dad_Loc,, $ammount) = explode(':', $string);
雨落星ぅ辰 2024-10-02 05:08:22

可以使用 sscanf() 直接实现解析可预测格式的字符串并将数值转换为非字符串数据类型。 演示

$str = 'location:32:DaD+LoC:102AD:Amount:294';
sscanf(
    $str,
    'location:%d:DaD+LoC:%[^:]:Amount:%d',
    $location,
    $dadloc,
    $amount
);
var_dump($location, $dadloc, $amount);

输出:

int(32)
string(5) "102AD"
int(294)

Parsing a predictably formatted string AND casting numeric values as non-string data types can be directly achieved with sscanf(). Demo

$str = 'location:32:DaD+LoC:102AD:Amount:294';
sscanf(
    $str,
    'location:%d:DaD+LoC:%[^:]:Amount:%d',
    $location,
    $dadloc,
    $amount
);
var_dump($location, $dadloc, $amount);

Output:

int(32)
string(5) "102AD"
int(294)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文