$string = 'Some string';
$pos = 5;
...??...
$begging // == 'Some s';
$end // == 'tring';
按给定位置将字符串一分为二的最佳方法是什么?
最佳答案
您可以使用 substr
获取两个子字符串:
$str1 = substr($str, 0, $pos);
$str2 = substr($str, $pos);
如果您省略第三个参数 length,substr
将获取字符串的其余部分。
但要获得结果,您实际上需要将 1 添加到 $pos
:
$string = 'Some string';
$pos = 5;
$begin = substr($string, 0, $pos+1);
$end = substr($string, $pos+1);
关于php - 按给定位置将字符串一分为二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3449348/