sprintf 警告 - 编码问题

发布于 2024-09-25 12:29:58 字数 1617 浏览 1 评论 0原文

我使用以下代码查找用户的所有属性,然后删除它们。我的问题是我收到警告:警告: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-10-02 12:29:58

%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' );

% is a special character in sprintf(). So you have to escape all % before processing it, %% is a literal %s.

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

You do not have to use sprintf here, you can use the concatenation operator too, like:

$delete = "http://admin:[email protected]/@api/users/=$user_id/properties/";
curl_fetch( $delete . $name2, 'admin', 'password' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文