LoginSignup
3
0

More than 5 years have passed since last update.

【メモ】PHPで複数の複数の文字で文字列を分割する

Last updated at Posted at 2018-02-12
<?php
//3語以上で分割する場合
function ex_explode($word_array, $str) {
    $return = array();

    //文字列を配列に入れる
    $array = array($str);

    //分割文字ごとにforeach
    foreach ($word_array as $value1){

        //文字列の配列を分割
        foreach ($array as $key => $value2) {
            $return = array_merge($return, explode($value1, $value2));

            //配列の最後になったら初期化
            if(count($array) - 1 === $key) {
                $array = $return;
                $return = array();
            }
        }
    }
    return $array;
}

//使う時

$word = 'タグ1,タグ2 タグ3 タグ4、タグ5';

ex_explode([' ',',','、',' ',],$word);

(参考)https://q-az.net/explode-php-extension/

3
0
4

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