1,'b'=>2,'c'=>'da,=cheng')如果有了1,弄出个2的方法,我"/> 1,'b'=>2,'c'=>'da,=cheng')如果有了1,弄出个2的方法,我">

Home >Backend Development >PHP Tutorial > 求个字符串切分正则解决办法

求个字符串切分正则解决办法

WBOY
WBOYOriginal
2016-06-13 13:09:26887browse

求个字符串切分正则
$var = "a=1,b=2,c='da,=cheng'";
1、把上面的字符串,切分成$var = array('a=1','b=2','c=da,=cheng')
2、最终目标是 $var = array('a'=>1,'b'=>2,'c'=>'da,=cheng')
如果有了1,弄出个2的方法,我的作法是用字符串分割函数,切割到第一个“=”

------解决方案--------------------

PHP code
$var = "a=1,b=2,c='da,=cheng'";
$arr=preg_split('/,(?=\w)/',$var,-1,PREG_SPLIT_NO_EMPTY);
foreach($arr as $v){
    $t=explode('=',$v,2);
    $ar[$t[0]]=$t[1];
}
print_r($ar); <div class="clear">
                 
              
              
        
            </div>
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