How can I split a delimited string into an array in PHP?

前端 未结 10 1654
失恋的感觉
失恋的感觉 2020-11-21 11:11

I need to split my string input into an array at the commas.

How can I go about accomplishing this?

Input:

9,admin@example.com,8
相关标签:
10条回答
  • 2020-11-21 12:05
    $string = '9,admin@google.com,8';
    $array = explode(',', $string);
    

    For more complicated situations, you may need to use preg_split.

    0 讨论(0)
  • 2020-11-21 12:05

    The Best choice is to use the function "explode()".

    $content = "dad,fger,fgferf,fewf";
    $delimiters =",";
    $explodes = explode($delimiters, $content);
    
    foreach($exploade as $explode) {
        echo "This is a exploded String: ". $explode;
    }
    

    If you want a faster approach you can use a delimiter tool like Delimiters.co There are many websites like this. But I prefer a simple PHP code.

    0 讨论(0)
  • 2020-11-21 12:10
    $myString = "9,admin@example.com,8";
    $myArray = explode(',', $myString);
    foreach($myArray as $my_Array){
        echo $my_Array.'<br>';  
    }
    

    Output

    9
    admin@example.com
    8
    
    0 讨论(0)
  • 2020-11-21 12:10

    Code:

    $string = "9,admin@example.com,8";
    
    $array  = explode(",", $string);
    
    print_r($array);
    
    $no = 1;
    foreach ($array as $line) {
        echo $no . ". " . $line . PHP_EOL;
        $no++;
    };
    

    Online:

    body, html, iframe { 
      width: 100% ;
      height: 100% ;
      overflow: hidden ;
    }
    <iframe src="https://ideone.com/pGEAlb" ></iframe>

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