我的猜是你有最後/單個項目的問題,因爲你正在從文件中讀取行,例如,通過fgets()並且你沒有從字符串中刪除尾部換行符。在這種情況下,你真的應該看看fgetcsv()。
無論如何,要修復你的函數將trim()應用到輸入字符串(或者如果你喜歡爆炸之後的所有數組元素),以刪除包括換行符在內的空格。
<?php
echo '--', foo('thisone', 'a,bcd,thisone,e'), "--\n";
echo '--', foo('thisone', 'thisone,e'), "--\n";
echo '--', foo('thisone', "e, thisone\n"), "--\n";
echo '--', foo('thisone', 'thisone'), "--\n";
echo '--', foo('thisone', ''), "--\n";
echo '--', foo('thisone', 'a,thisone,b,thisone,c,thisone'), "--\n";
function foo($deleteuser, $userList) {
$pieces = array_map('trim', explode(',', $userList));
foreach(array_keys($pieces, $deleteuser) as $key) {
unset($pieces[$key]);
}
return implode(',', $pieces);
}
打印
--a,bcd,e--
--e--
--e--
----
----
--a,b,c--
我用array_keys代替array_search(),以防萬一用戶名可以出現比在列表中一次。
你知道,PHP有一個[內置的csv解析器](http://php.net/manual/en/function.fgetcsv.php)。可能更好地使用,通過使用'explode' – troelskn 2010-08-26 08:20:14
對不起 - 傢伙 - 修改了標題,使更多的意義......我遇到了問題,當它是列表上的「最後」(==只)項目,我看到刪除那... – tzmatt7447 2010-08-26 08:47:21