开发者

separate string in two by given position

开发者 https://www.devze.com 2023-01-11 11:55 出处:网络
$string = \'Some strin开发者_如何学Gog\'; $pos = 5; ...??... $begging // == \'Some s\'; $end // == \'tring\';
$string = 'Some strin开发者_如何学Gog';
$pos = 5;

...??...

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

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


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);


Regex solution (if you are into it):

...
$string = 'Some string xxx xxx';
$pos = 5;

list($beg, $end) = preg_split('/(?<=.{'.$pos.'})/', $string, 2);

echo "$beg - $end";

Regards

rbo


How about substr()?

$string = 'Some string';
$pos = 5;

$beginning = substr($string, 0, $pos);
$end = substr($string, $pos);


Wordwrap works better in my opinion.

$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;

The above example will output:

The quick brown fox<br />
jumped over the lazy<br />
dog.

Another example:

$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);

echo "$newtext\n";

The above example will output:

A very
long
wooooooo
ooooord.


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.


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


<?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).


<?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

精彩评论

暂无评论...
验证码 换一张
取 消