2010-10-02 164 views
0

我正在使用以下代碼來查找用戶的所有屬性,然後刪除它們。我的問題是,我收到警告:警告:sprintf():每個屬性的參數太少sprintf警告 - 編碼問題

然而,當我手動輸入$ USER_ID進行刪除字符串作爲FIRST_LAST %% 40ourwiki.com它的作品!

似乎像sprintf需要雙'%'但不知道爲什麼。有沒有辦法解決這個問題?此外,我使用file_get_contents相同的變量,這工作正常。

驗證碼:

$user="[email protected]"; 
$user_id=str_replace(array('@', '#'), array('%40', '%23'), $user); 
print $user_id; 

$url=("http://admin:[email protected]/@api/users/=$user_id/properties"); 
$xmlString=file_get_contents($url); 

$delete = "http://admin:[email protected]/@api/users/=$user_id/properties/%s"; 
$xml = new SimpleXMLElement($xmlString); 

function curl_fetch($url,$username,$password,$method='DELETE') 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); 
    return curl_exec($ch); 
} 

foreach($xml->property as $property) { 
    $name = $property['name']; 
    $name2=str_replace(array('@', '#'), array('%40', '%23'), $name); 
    print $name2; 
    curl_fetch(sprintf($delete, $name2),'admin','password'); 
} 

提前感謝!

回答

0

%sprintf()中的特殊字符。所以你必須在處理它之前逃脫所有%%%是一個文字%s

$delete = str_replace("http://admin:[email protected]/@api/users/=$user_id/properties/", '%', '%%').'%s'; 

你不必在這裏使用sprintf,您可以使用連接運算符太像:

$delete = "http://admin:[email protected]/@api/users/=$user_id/properties/"; 
curl_fetch($delete . $name2, 'admin', 'password'); 
+0

非常感謝你,這似乎是爲我工作:) – Aaron 2010-10-02 19:26:36