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');
}
提前致谢!
I'm using the following code to find all properties for a user and in turn delete them. My problem is that I'm getting a warning: Warning: sprintf(): Too few arguments for each of the properties.
However, when I manually enter the $user_id for the delete string as first_last%%40ourwiki.com it works!
Seems like sprintf requires double '%' but not sure why. Is there a way to get around this? Also, I'm using the same variable for file_get_contents and this works fine.
The Code:
$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');
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
%
是sprintf() 中的特殊字符
。因此,在处理之前必须对所有%
进行转义,%%
是一个文字%s
。您不必在这里使用 sprintf,您也可以使用串联运算符,例如:
%
is a special character insprintf()
. So you have to escape all%
before processing it,%%
is a literal%s
.You do not have to use
sprintf
here, you can use the concatenation operator too, like: