Php 按给定位置将字符串一分为二

Php 按给定位置将字符串一分为二,php,string,Php,String,按给定位置将字符串一分为二的最佳方法是什么 最好的分离方法是什么 按给定位置将字符串一分为二 如果我理解正确,您可以: $string = 'Some string'; $pos = 5; ...??... $begging // == 'Some s'; $end // == 'tring'; 结果: $str = 'hello world'; $pos = 5; $separated_str = substr($str, $pos); echo $separated_str; 这取

按给定位置将字符串一分为二的最佳方法是什么

最好的分离方法是什么 按给定位置将字符串一分为二

如果我理解正确,您可以:

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

...??...

$begging // == 'Some s';
$end // == 'tring';
结果:

$str = 'hello world';
$pos = 5;

$separated_str = substr($str, $pos);
echo $separated_str;
这取决于字符串的结构,因为还有其他方法可以拆分字符串。

您可以使用来获取两个子字符串:

world
如果省略第三个参数长度,
substr
将获取字符串的其余部分

但要获得结果,实际上需要在
$pos
中添加一个:

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

正则表达式解决方案(如果您喜欢):

。。。
$string='Some string xxx xxx';
$pos=5;
在我看来,list($beg,$end)=preg_split('/(?更有效

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

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

echo "$beg - $end";
上述示例将输出:

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

echo "$newtext\n";

我在寻找一种在给定位置拆分字符串的快速方法

为什么是一个给定的位置?通常您需要根据一个字符进行拆分(我认为您是按位置搜索的,因为explode函数会将字符串拆分为太多的片段):

我的解决方案最终是:

A very
long
wooooooo
ooooord.
注意,我放弃了使用“position”(我使用“substr”来获取它)

考虑到您对“位置”的需求并不是真正必要的,我的解决方案是上述所有解决方案中最快的,并且没有多字节问题。而且我认为它更易于阅读

不幸的是,该函数在上一个PHP中被弃用,如果你有旧的PHP,你可以使用它,或者你可以使用preg_split。我对此不太满意,因为实现相同功能的唯一方法是滥用正则表达式。如果你不能使用
split
最快的替代方法是:

$string = "hello world my dear";
list ($first, $remaining ) = split (' ', $string, 2); //limit to 2 pieces
echo $first; //"hello";
echo $remaining; //"world my dear";
wich比@rubble answer快得多,而且根本不使用位置

参考:


这个位置是怎么来的?你是根据
$string
计算出它的值的吗?这里给出了不同的答案。你所说的“最佳方式”是什么意思?最快?最少的代码?处理多字节字符串时要小心!使用
$pos+1
你忽略了一个字符。如果$pos为5,你将得到
“一些”
“tring”
@Gumbo我的第一反应是对Tim的回答添加一条类似的评论,告诉他他没有使用
$pos+1
:)+1作为固定答案是错误的。哦,我明白了,如果我回答一个PHP问题,并且不重复显而易见的解决方案(到目前为止给出了相同的四次)试着展示另一种选择,我会被同行们否决。很好!只有一种方法可以解决这个问题…快!有人做一个基准性能测试并比较结果!我喜欢一行代码:3在我的测试中,正则表达式比substr快5倍(7.7009201049805E-5比0.12504050611145毫秒)@这是因为人们不理解它。不要因为它而大发雷霆,你的解决方案很棒。我喜欢在代码中保存行数的人。谢谢这不适用于OP问题“按给定位置将字符串分成两部分”你也说过,“这取决于字符串的结构,因为还有其他方法可以拆分字符串”,您可以重新编辑吗?
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);

echo "$newtext\n";
A very
long
wooooooo
ooooord.
$string = "hello world my dear";
list ($first, $remaining ) = split (' ', $string, 2); //limit to 2 pieces
echo $first; //"hello";
echo $remaining; //"world my dear";
$string = "hello world my dear";
list ($first, $remaining ) =  preg_split('/\s+/', $string, 2);
<?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);
}
<?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
?>