怎么吧一个字符串冒号前面的值当做数组的键,把冒号后面的值当做数组的值

2017-04-11 11:14:51 +08:00
 dadazhang

Domain Name: dada.com,
Registry Domain ID: 102993_DOMAIN_COM-VRSN,
Registrar WHOIS Server: whois.pairNIC.com,
Registrar URL: https://www.pairNIC.com,
Updated Date: 2014-02-03T19:00:24+0000Z,
Creation Date: 1995-03-04T05:00:00+0000Z,
Registrar Registration Expiration Date: 2018-03-05T05:00:00+0000Z,
Registrar: pairNIC INC,
Registrar IANA ID: 99,
Registrar Abuse Contact Email: abuse@pairnic.com,
Registrar Abuse Contact Phone: +1.8887247642,
Domain Status: clientDeleteProhibited,
Domain Status: clientTransferProhibited,
Domain Status: clientUpdateProhibited,
Registry Registrant ID: ,
Registrant Name: Juan-Carlos Sobrino,
Registrant Organization: ,
Registrant Street: 47 Saint Mark's Place ,
Registrant City: New York,
Registrant State/Province: NY,
Registrant Postal Code: 10003,
Registrant Country: US,
Registrant Phone: +1.6465551234,
Registrant Phone Ext:,

就类似这样的数据,有的冒号后面还是空的值。。。怎么解决。。。。求助

2804 次点击
所在节点    PHP
10 条回复
silentoy
2017-04-11 11:31:14 +08:00
```php
$data = explode("\n", $str);
$res = array();

foreach ($data as $item) {
list($key, $value) = explode(":", $item);
$res[trim($key)] = trim($value, ' ,');
}

var_dump($res);
```
kghch
2017-04-11 11:39:19 +08:00
lines = data.split(',')
res = {}
for line in lines:
line= line.split(':')
k = line[0]
v = ''
if len(line) > 1:
v = line[1]
res[k] = v

print res
dadazhang
2017-04-11 11:59:15 +08:00
@silentoy 谢谢,完美解决
kghch
2017-04-11 12:55:47 +08:00
#2 ,格式丢了,不好意思。
nfroot
2017-04-11 14:03:06 +08:00
php 的数组 key 还能用空格啊
wangjie
2017-04-11 14:38:16 +08:00
let a={}
data.split(',')
.map((t)=>{
let l=t.split(':')
a[l[0]]=l[1]
})
console.log(a)
Tyanboot
2017-04-11 23:44:39 +08:00
…楼上用 Python 和 JS 回答的是在想啥。
jea
2017-04-18 13:36:22 +08:00
@Tyanboot 论语言的多样性
Balthild
2017-04-19 10:55:17 +08:00
@jea 这里是 /go/php
jea
2017-04-19 12:53:59 +08:00
@Balthild 我的意思是他们[手动滑稽]

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/354017

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX