Shortcut for: $foo = explode(“ ”, “bla ble bli”); echo $foo[0]

后端 未结 9 1411
清歌不尽
清歌不尽 2020-12-19 15:48

is there a way to get the n-th element of a splitted string without using a variable?

My PHP code always looks like this:

$foo = explode(\" \", \"bla         


        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 16:11

    close. the right track is making a function or method for something that gets repeated.

    function extract_word($input, $index) {
        $input = explode(' ', $input);
        return $input[$index];
    }
    

    add a third argument of $separater = ' ' if you may have different word separaters.

提交回复
热议问题