勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

PHPでゼロパディング

   2014/07/03  PHP

予め桁数を決めておいて、桁数未満の場合は0で埋めたい場合の処理。

str_pad(ゼロパディングしたいもの,桁数,埋めたい数字や文字,STR_PAD_LEFT);

$hoge = 125;
$hoge = str_pad($hoge, 8, 0, STR_PAD_LEFT);
echo $hoge;

実行結果は 00000125 になる。

■参考サイト様

http://d.hatena.ne.jp/naohide_a/20080224/1203866206
http://php.style-mods.net/tips_php/tips_php_2_05.htm

 - PHP

  関連記事

PHPで「Warning: date(): It is not safe to rely on the system's timezone settings.」のエラー

PHPでdate関数を使った際に、   Warning: date() ...

PHPでサイトURLからtitleとRSS用URLを取得

サイトのURLからRSS用URLとサイトタイトルを自動で取得したかった。その方法 ...

setcookieで「Cannot modify」エラー

PHPでsetcookieを使うと「Warning: Cannot modify ...

MySQLiでDELETE文

MySQLiでDELETE文 ■ソース $mysqli = new mysqli ...

isset等、変数を調べる際の注意点

$strがNULLだったり0だったり空文字だったりする場合で、 if($str) ...