How can I split a list with multiple delimiters?

前端 未结 3 403
不知归路
不知归路 2021-01-25 03:59

Basically, I want to enter text into a text area, and then use them. For example

variable1:variable2@variable3
variable1:variable2@variable3
variable1:variable2@         


        
相关标签:
3条回答
  • 2021-01-25 04:40

    Besides preg_split:

    $line = 'variable11:variable12@variable13';
    print_r(preg_split('/[:@]/', $line));
    
    /*
    Array
    (
        [0] => variable11
        [1] => variable12
        [2] => variable13
    )
    */
    

    you could do a preg_match_all:

    $text = 'variable11:variable12@variable13
    variable21:variable22@variable23
    variable31:variable32@variable33';
    
    preg_match_all('/([^\r\n:]+):([^\r\n@]+)@(.*)\s*/', $text, $matches, PREG_SET_ORDER);
    
    print_r($matches);
    
    /*
    Array
    (
        [0] => Array
            (
                [0] => variable11:variable12@variable13
    
                [1] => variable11
                [2] => variable12
                [3] => variable13
            )
    
        [1] => Array
            (
                [0] => variable21:variable22@variable23
    
                [1] => variable21
                [2] => variable22
                [3] => variable23
            )
    
        [2] => Array
            (
                [0] => variable31:variable32@variable33
                [1] => variable31
                [2] => variable32
                [3] => variable33
            )
    
    )
    */
    
    0 讨论(0)
  • 2021-01-25 04:53

    if necessary, you could make several calls to "explode"

    http://jp.php.net/manual/en/function.explode.php

    0 讨论(0)
  • 2021-01-25 05:01

    try preg_split http://php.net/manual/en/function.preg-split.php

    0 讨论(0)
提交回复
热议问题