【问题标题】:PHP - sprintf with multiple url valuesPHP - 具有多个 url 值的 sprintf
【发布时间】:2023-03-20 14:45:01
【问题描述】:

我正在使用sprintf() PHP 函数并尝试将这个 url 格式化为:

"/api/v1/post/1/225883"

我有:

const API_URL = '/api/v1';
const POST_ENDPOINT = '/post';
$this->client->getId()
$this->post->getDestinationId()

我试过了:

sprintf(self::API_URL . self::POST_ENDPOINT,
        $this->client->getId(),
        $this->post->getDestinationId());

我得到:/api/v1/post

有人可以帮忙吗?

【问题讨论】:

    标签: php url printf


    【解决方案1】:

    您缺少格式字符串:

    $url = sprintf("%s%s/%s/%s", self::API_URL, self::POST_ENDPOINT,
        $this->client->getId(),
        $this->post->getDestinationId());
    

    【讨论】:

    • 非常感谢! @Barmar
    • 虽然我建议用%d 替换最后两个%s 以缩小IDS 的数据类型
    • @JanMyszkier 也许,但如果它们有重要的前导零,那将不起作用。
    猜你喜欢
    • 2021-12-28
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多