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

标签 php string

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

...??...

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

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

最佳答案

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

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

如果您省略第三个参数 lengthsubstr 将获取字符串的其余部分。

但要获得结果,您实际上需要将 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/

相关文章:

php - 无法使用 PHP、Mysql 和 Angular.js 检查数据库中的重复数据

php simplexml xpath按属性匹配元素并按文本值匹配子元素

php - 谷歌 OAuth 2 身份验证 - 错误 : redirect_uri_mismatch even after registered uri

java - 通过第一个和最后一个字符删除字符串的一部分?

java - 使用 compareTo(String) 得到错误说 undefined for the type String

php - 停止 WordPress 从 301 重定向/index.php 到/

php - codeigniter 中的自动完成

php - 如何访问此 Javascript 数组(JSON 对象?)?

java - 获得一系列结果的最佳方法是什么?

c - 在 C 中为字符串数组动态分配内存