这样的函数应该怎么写<?php
function test($n){
//字符串str
$str="tab_user.name=admin,tab_user.password=123,tab_user.type=1,";

//待补程序
........
}echo test("tab_user.name"); //显示admin
echo test("tab_user.password"); //显示123
echo test("tab_user.type"); //显示1
?>

解决方案 »

  1.   

    function test($n){
    //字符串str
    $str="tab_user.name=admin,tab_user.password=123,tab_user.type=1,";//待补程序
    $arr = explode(',', $str);
    foreach($arr as $val){
    if(strpos($val, $n) !== false){
    $ar = explode('=', $val);
    echo $ar[1], '<br>';
    break;
    }
    }
    }echo test("tab_user.name"); //显示admin
    echo test("tab_user.password"); //显示123
    echo test("tab_user.type"); //显示1
      

  2.   

    用explode将字符串分割为数组
    function test($n){
        $str="tab_user.name=admin,tab_user.password=123,tab_user.type=1,";
        $arr = explode($str, ",");
        foreach($arr as $v){
            $tmp = explode($v, "=");
            $results[$tmp[0]] = $tmp[1];
        }
        return $results[$n];
    }
      

  3.   

    <?php
    function test($n) {
       $str="tab_user.name=admin,tab_user.password=123,tab_user.type=1,";
       $arr = explode(',',$str);
       $arr2 = array();
       while(list($key,$val) = each($arr)) {
          $tmp = explode('=',$val);
          $tmp[1] = str_replace(',','',$tmp[1]);  //删除元素中存在的逗号
          $arr2[$tmp[0]] = $tmp[1];
       }
       echo $arr2[$n];
    }
    test('tab_user.type');
    ?>
      

  4.   

    貌似这一句是多余的。。:
    $tmp[1] = str_replace(',','',$tmp[1]);  //删除元素中存在的逗号
      

  5.   

    试过,都很好用!!  谢谢大家。。
    不过2楼的explode($str, ",")位置写反了,换下位置就可以了^^