PHP-格式化字符串输出
我正在尝试根据分配给用户的值操作文本字符串 全文字符串为“列表、项目、案例、标识和位置”,仅针对管理员显示全文 系统上的每个用户都有访问权限。每一项权利决定了他们能做什么和不能做什么。 所以访问权限PHP-格式化字符串输出,php,string,Php,String,我正在尝试根据分配给用户的值操作文本字符串 全文字符串为“列表、项目、案例、标识和位置”,仅针对管理员显示全文 系统上的每个用户都有访问权限。每一项权利决定了他们能做什么和不能做什么。 所以访问权限a1='列表',b1='项目'等等 当用户登录时,页面显示他们有权访问的内容 例如: 对于管理员用户。。这看起来像: Welcome Admin You have access to Lists, Items, Cases, Identity and Location 所以现在我试着做以下几点: i
a1='列表'
,b1='项目'
等等
当用户登录时,页面显示他们有权访问的内容
例如:
对于管理员用户。。这看起来像:
Welcome Admin
You have access to Lists, Items, Cases, Identity and Location
所以现在我试着做以下几点:
if ($user == 'admin') $foo = 'Lists, Items, Cases, Identity and Location'; // full access
但我需要能够更改每个用户的值
$foo ='';
if (strpos($access,'a1') !== false) $foo .= 'Lists ';
if (strpos($access,'b1') !== false) $foo .= 'Items ';
if (strpos($access,'c1') !== false) $foo .= 'Cases ';
if (strpos($access,'d1') !== false) $foo .= 'Identity ';
if (strpos($access,'e1') !== false) $foo .= 'Location ';
我的问题是标点符号被弄乱了。
用户Miz可以访问a1、b1、c1
当她登录时,她得到:
Welcome Miz
You have access to Lists Items Cases
我想说的是:
Welcome Miz
You have access to Lists, Items and Cases
有人能建议实现这一目标的最佳方法吗
谢谢使用
尝试使用数组并加入:
$rights = array();
$foo = '';
if (strpos($access, 'a1') !== false)
$rights[] = 'Lists ';
if (strpos($access, 'b1') !== false)
$rights [] = 'Items ';
if (strpos($access, 'c1') !== false)
$rights [] = 'Cases ';
if (strpos($access, 'd1') !== false && strpos($access, 'e1') !== false) {
$rights[] = "Identity and Location";
} else {
if (strpos($access, 'd1') !== false)
$rights[] = 'Identity ';
if (strpos($access, 'e1') !== false)
$rights[] = 'Location ';
}
echo join(', ', $rights);
编辑:根据其他答案中的OP评论,
身份和位置
已处理。类似的操作将起作用
$foo ='';
if (strpos($access,'a1') !== false) $foo .= 'Lists, ';
if (strpos($access,'b1') !== false) $foo .= 'Items, ';
if (strpos($access,'c1') !== false) $foo .= 'Cases, ';
if (strpos($access,'d1') !== false) $foo .= 'Identity, ';
if (strpos($access,'e1') !== false) $foo .= 'Location, ';
$str = rtrim($foo,', '); //remove last ","
echo preg_replace('/,\s([^,]*$)/', ' and $1', $str); //replace last ", " with " and "
使用数组保存列表并通过在数组上迭代来打印,您可以根据当前项目编号应用分隔符。您可以添加一个包含用户可以执行的所有操作的数组。例如,列表、项目、案例、标识、位置。在该数组中,还添加“a1”作为键或值。你喜欢什么都行。循环指定所需用户的数组,并将这些值填充到另一个数组中。然后简单地使用爆炸或内爆(不知道是哪一个,我总是忘了…)在阵列和吊杆上。你得到了逗号,如果你想做“和”的事情,循环数组,看看它是否是最后一个。如果是这样,请添加“and”和“there,else”,“。基本上是正确的,但它不包括以下内容:
列表、项目和案例
@SyedQarib检查我的答案。@SyedQarib,谢谢你的建议。这对我帮助很大。我认为麦克曼问这个好问题也应该得到+1分。
$rights = array();
$foo = '';
if (strpos($access, 'a1') !== false)
$rights[] = 'Lists ';
if (strpos($access, 'b1') !== false)
$rights [] = 'Items ';
if (strpos($access, 'c1') !== false)
$rights [] = 'Cases ';
if (strpos($access, 'd1') !== false && strpos($access, 'e1') !== false) {
$rights[] = "Identity and Location";
} else {
if (strpos($access, 'd1') !== false)
$rights[] = 'Identity ';
if (strpos($access, 'e1') !== false)
$rights[] = 'Location ';
}
echo join(', ', $rights);
$foo ='';
if (strpos($access,'a1') !== false) $foo .= 'Lists, ';
if (strpos($access,'b1') !== false) $foo .= 'Items, ';
if (strpos($access,'c1') !== false) $foo .= 'Cases, ';
if (strpos($access,'d1') !== false) $foo .= 'Identity, ';
if (strpos($access,'e1') !== false) $foo .= 'Location, ';
$str = rtrim($foo,', '); //remove last ","
echo preg_replace('/,\s([^,]*$)/', ' and $1', $str); //replace last ", " with " and "