2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでの文字列の操作

Last updated at Posted at 2017-12-21

学んだことの覚書

  • 文字列の連結
sample.php
 print 'The number is '.'723';
 // 出力: The number is 723
  • trim():余白の削除
sample.php
 print trim(banana );
 // 出力: banana(余白がなくなる)
  • strlen():文字列の長さの取得
sample.php
 print trim(banana);
 // 出力: 6
  • strcasecmp():大文字小文字の区別なく文字列比較
sample.php
 if(strcasecmp('BANANA','banana') == 0)
 {
   print 'same';
 }
 // 出力: same
  • strtolower():すべて小文字に
  • strtoupper():すべて大文字に
sample.php
 print strtolower('Yamada');
 // 出力: yamada
 print strtoupper('Yamada');
 // 出力: YAMADA
  • ucwords():最初の文字だけ大文字に(小文字文字列のみ)
sample.php
 print ucwords('yamada');
 // 出力: Yamada
  • substr():文字の一部のみ取り出す
sample.php
 print substr('Yamada',0,2);
 // 出力: Ya

 print substr('Yamada',-2,2);
 // 出力: da

 print substr('Yamada',-2);
 // 出力: da
 // 第三引数を省略すると自動的に最後の文字列までを抽出

  • str_replace():文字の一部を置換
sample.php
 $before = 'Yamada';
 $after = str_replace('Ya','Ha',$before);
 print $after
 // 出力: Hamada
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?