我不得不製作一個GUI,它允許我的客戶端自定義一個問候消息。他們明白他們將不得不使用%s來指示用戶名的顯示位置。但是,如果指定%s太多,sprintf
將失敗,並顯示消息Too few arguments
。如何讓sprintf優雅地失敗?
是否有一個選項可以在字符串中保留多餘的%s實例,或者只是將它們替換爲空字符串?
// $greeting_template === 'Welcome to our site %s. Broken: %s'
$output = sprintf($greeting_template, $user_name);
顯然還有其他的sprintf格式模板,這些模板也應該優雅地失敗。
我嘗試使用'@ sprintf'來忽略警告,但它似乎沒有做任何事情。 –