separate string in two by given position

前端 未结 8 1092
萌比男神i
萌比男神i 2020-12-03 17:14
$string = \'Some string\';
$pos = 5;

...??...

$begging // == \'Some s\';
$end // == \'tring\';

What is the best way to separate string in two by

相关标签:
8条回答
  • 2020-12-03 17:45

    How about substr()?

    $string = 'Some string';
    $pos = 5;
    
    $beginning = substr($string, 0, $pos);
    $end = substr($string, $pos);
    
    0 讨论(0)
  • 2020-12-03 17:51

    What is the best way to separate string in two by given position?

    If i understand you correctly, you can do:

    $str = 'hello world';
    $pos = 5;
    
    $separated_str = substr($str, $pos);
    echo $separated_str;
    

    Result:

    world
    

    This depends on the structure of your string as there are other ways also to split the string.

    0 讨论(0)
  • 2020-12-03 17:51

    I was searching for a fast a way to split a string at a given position.

    Why a given position? Usually what you need is to split against a character (I think you searched by position because the explode function would split the string in too much pieces):

    My solution was finally:

    $string = "hello world my dear";
    list ($first, $remaining ) = split (' ', $string, 2); //limit to 2 pieces
    echo $first; //"hello";
    echo $remaining; //"world my dear";
    

    Notice that I dropped the use of "position" (I was using "substr" to get it).

    Given that your need for "position" was not really necessary, my solution is the fastest among all others above and does not have multi-byte issues. Also I think it is much easier to read.

    Unluckily that function is deprecated in last PHP, you can use it if you have old PHP or you can use preg_split. I was not very happy of that because the only way to achieve same functionality is abusing regex. In case you cannot use split the fastest alternative is:

    $string = "hello world my dear";
    list ($first, $remaining ) =  preg_split('/\s+/', $string, 2);
    

    wich is much faster than @rubber answer and does not use position at all.

    Reference: Split documentation

    0 讨论(0)
  • 2020-12-03 17:54
    <?php
    Function Splp($S,...$A){ #Split At Positions 
        $k = 0;
        $R = [];
        $A[] = Strlen($S);
        Foreach($A As $a){
            $R[] = Substr($S,$k,$a-$k);
            $k = $a;
        }
        Return $R;}
    
    $string = 'Some string';
    $pos1 = 5;
    $pos2 = 7;
    [$Start,$Mid,$End] = Splp($string,$pos1,$pos2);
    echo $Start,', ',$Mid,', ',$End; #  Some , st, ring
    ?>
    
    0 讨论(0)
  • 2020-12-03 18:02

    You can use substr to get the two sub-strings:

    $str1 = substr($str, 0, $pos);
    $str2 = substr($str, $pos);
    

    If you omit the third parameter length, substr takes the rest of the string.

    But to get your result you actually need to add one to $pos:

    $string = 'Some string';
    $pos = 5;
    $begin = substr($string, 0, $pos+1);
    $end = substr($string, $pos+1);
    
    0 讨论(0)
  • 2020-12-03 18:03
    <?php
    $string = 'Some string';
    $pos = 6;
    $number_of_pieces = 2;
    list($beginning, $end) = split_into_pieces($string, $pos, $number_of_pieces);
    // $beginning === 'Some s'; $end === 'tring'
    
    function split_into_pieces($string, $length, $limit = 0) {
      --$length;
      return preg_split("/(?<=^..{{$length}}|(?!^)\\G.{{$length}})/", $text, $limit);
    }
    
    • $string is the string to split
    • $length is the size of each piece in Unicode code-points (or ASCII characters if not using the UTF-8 option, see below). Any value less than 1 will return the entire input string as a single piece
    • $limit is the maximum number or pieces, or 0 for unlimited. The final piece will contain the remainder of the string and might be longer or shorter than $length

    The regex that is doing the magic is using a positive look-behind (?<= ... ). It looks for ^, which is the start of the string, | or if it's not at the beginning of the string (?!^) it looks for \G, which is the position of the previous match. . is any character, { n } is repeated n times, where n is {$length}. When using \G, it adds an extra character for some reason, which is why there's the line --$length; and why the first match has an extra . to search for an extra code-point, which is not usually allowed in look-behinds. I think the zero-width assertions ^ and \G are anchoring the pattern, allowing different lengths in the look-behind.

    The extra { and } around $length are necessary to stop the regex braces being interpreted as an escaped PHP variable. /su are the regex options. The /s option says allow . to match newline characters. The /u option says to match Unicode code-points encoded as UTF-8 (remove u if you are parsing strings that are non-UTF-8 compliant).

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