0
1

More than 3 years have passed since last update.

🐘【PHP】逆順forループで文字列を読んでいく

Posted at

環境

PHP 7.2.21

やりたいこと

単純な文字列を後ろから一文字ずつ読んで色々判定したいので
後ろから一文字ずつのforループを実装してみる

やったこと

forの開始位置をstrlen()で文字数取得し$i--で逆に回す
※マルチバイト文字に対応していないのでそこは別途なんとかしたい

forLoop.php
        // 対象の文字列
        $tmpString = 'eltociear_hoge';

        for ($i = strlen($tmpString) -1; $i > -1; $i--) {
            // 一文字を表示
            var_dump($tmpString[$i]);
        }

出力結果

string(1) "e"
string(1) "g"
string(1) "o"
string(1) "h"
string(1) "_"
string(1) "r"
string(1) "a"
string(1) "e"
string(1) "i"
string(1) "c"
string(1) "o"
string(1) "t"
string(1) "l"
string(1) "e"
0
1
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
1