2017-08-25 10 views
0

を示して、私はちょうどそれが00033を表示したいが、それは私はそれが00033-6、 になっていない場合は、間違ったprintf('%50d-',33)を試してみてください000335.PHPのprintf 5桁の余分な1桁

を示し 、タイトルとしてprintf('%50d',33)に関する問題を発見しました最後の数字は総桁数です。

これを削除する方法は分かりますか?

EDITED

モデル/ Product.php

class Product extends Model 
{ 
    protected $appends = ['code']; 
    public function getCodeAttribute(){ 
     return $this->attributes['code'] = sprintf("A%'.05d",$this->id); 
    } 
} 

ビュー/ home.blade.php

<ul class='codeList'> 
 
    @foreach($products as $product) 
 
    <li> 
 
    <div class='name'>{{ $product->name }}</div> 
 
    <div class='code'>{{ $product->code }}</div> {{-- This Part Show A00033 --}} 
 
    </li> 
 
    @endforeach 
 
</ul>

+0

書式文字列*のコンポーネントを正しい順序*、[マニュアルの指定に従って]指定する必要があります(http://php.net/manual/en/function.sprintf.php)。 –

+0

@MattGibsonご返信ありがとうございます、私は誤って間違ったコードをコピーします。 私のソースはprintf( "A% '。05d"、$ this-> id)なのでtypoです。 –

答えて

0

問題を解決しました。 使用sprintf("A%'.05d",$this->id); instent of printf("A%'.05d",$this->id);

ありがとう@Matt for the link。 更新質問票に回答してください。

0

あなたのフォーマット文字列がMattによってmentionedとして正しい順序ではないように見えます。 printf("%'.05d\n",33);

+0

お手伝いをしてくれてありがとう。 実際に私のソースにある私の誤植は、show printf( 'A%05d'、$ this-> id)です。 と私はprintf( "% '。05d \ n"、$ this-> id)を試してみました。やはり同じ1つの数字が同じです。 –