Home >Backend Development >PHP Tutorial > php 字符串操作,该怎么解决

php 字符串操作,该怎么解决

WBOY
WBOYOriginal
2016-06-13 12:51:17784browse

php 字符串操作

$getstr="id=189,pc=0,pt=0,valide=1\r\nid=176,pc=0,pt=0,valide=1\r\nid=177,pc=0,pt=0,valide=1\r\n";



我想以 \r\n 的形式来划分字符串,得到一个数组,然后用键值对的形式来取值,
比如取第一个id的值
$getid=$arr["id"]

求大牛指点


------解决方案--------------------
$getstr = "id=189,pc=0,pt=0,valide=1\r\nid=176,pc=0,pt=0,valide=1\r\nid=177,pc=0,pt=0,valide=1\r\n";<br />
<br />
foreach(explode("\r\n", $getstr) as $row) {<br />
  if($row) {<br />
    parse_str(strtr($row, ',', '&'), $t);<br />
    $res[] = $t;<br />
  }<br />
}<br />
<br />
echo $res[0]['id'];
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn