Help us understand the problem. What is going on with this article?

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

学習記録

前提条件

・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
ke_113
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした