LoginSignup
0
0

More than 3 years have passed since last update.

PHP関数~文字列を扱う関数~

Last updated at Posted at 2020-04-26

学習記録

前提条件

・PHP version 5.4

文字列を扱う関数

・strlen / mb_strlen
・strpos / mb_strpos
・str_replace
・trim

strlen/mb_strlen

文字をバイト数調べる

書式
$変数=strlen(調べたい文字列);

//日本語対応
$変数=mb_strlen(調べたいの文字列);
builtin.php
$input = 'string';
echo strlen($input);
//結果 : 6

$input = 'こんにちは';
echo mb_strlen($input);
//結果 : 5

strpos/mb_strpos

文字列からある文字を探す
もし探したい文字列がなかったらfalseを返す

書式
$変数=strpos(文字列,'探したい文字');

//日本語対応
$変数=mb_strpos(文字列,'探したい文字');
builtin.php
$input = 'string';
echo strlen($input, 't');
//結果 : 1

$input = 'こんにちは';
echo mb_strlen($input, 'に');
//結果 : 2

str_replace

文字列を置換する

書式
$変数=str_replace('文字列にある文字' , '置換したい文字' , 文字列); //日本語にも対応できる
builtin.php
$input = 'こんにちは';
echo mb_strlen('にち' , 'ばん' , $input);
//結果 : こんばんは

trim

文字列の先頭及び末尾にある空白や改行を除去できる

書式
$変数=trim(文字列);
builtin.php
$input = ' string  ';
echo strlen($input);
//結果 : string
0
0
2

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
0
0