How to split a string by multiple delimiters in PHP?

前端 未结 4 1225
温柔的废话
温柔的废话 2020-11-28 13:16

\"something here ; and there, oh,that\'s all!\"

I want to split it by ; and ,

so after processing should get:

相关标签:
4条回答
  • 2020-11-28 13:46

    You can get the values into an array using Devin's or Meder's method.

    To get the output you want, you could probably do this

    echo implode("\n", $resultingArray);
    

    Or use <br /> if it's HTML you want.

    0 讨论(0)
  • 2020-11-28 13:55
    <?php
    
    $pattern = '/[;,]/';
    
    $string = "something here ; and there, oh,that's all!";
    
    echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
    

    Updated answer to an updated question:

    <?php
    
    $pattern = '/[\x{ff0c},]/u';
    
    //$string = "something here ; and there, oh,that's all!";
    $string = 'hei,nihao,a ';
    
    
    echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
    
    0 讨论(0)
  • 2020-11-28 13:57
    $result_array = preg_split( "/[;,]/", $starting_string );
    
    0 讨论(0)
  • 2020-11-28 14:00

    The split() PHP function allows the delimiter to be a regular expression. Unfortunately it's deprecated and will be removed in PHP7!

    The preg_split() function should be OK, and it returns an array:

    $results = preg_split('/[;,]/', $string);
    

    There are a few extra optional parameters which may be useful to you.

    Is the first delimiter character in your edited example actually a 2 byte Unicode character?

    Perhaps the preg_slit() function is treating the delimiter as three characters and splitting between the characters of the unicode (Chinese?) 'character'

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