这样的函数应该怎么写<?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
?>
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
?>
//字符串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
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];
}
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');
?>
$tmp[1] = str_replace(',','',$tmp[1]); //删除元素中存在的逗号
不过2楼的explode($str, ",")位置写反了,换下位置就可以了^^