我有字符串1. Welcome。
我想将字符串拆分为 2 个字符串:1 & Welcome。
我将如何使用substr PHP 函数来实现这一点?
非常感谢您的任何指点
我有字符串1. Welcome。
我想将字符串拆分为 2 个字符串:1 & Welcome。
我将如何使用substr PHP 函数来实现这一点?
非常感谢您的任何指点
对于这种特殊情况,最合适的功能是explode
:
$str = '1. Welcome';
$parts = explode('. ', $str);
// explode returns a numerically indexed array, so:
$number = $parts[0];
$title = $parts[1];
您可以将其与list
构造结合起来以获得额外的便利:
list($number, $title) = explode('. ', $str);
最后,如果标题包含点,最好指定explode
(限制生成的令牌数量)的第三个参数:
list($number, $title) = explode('. ', $str, 2);
http://php.net/manual/en/function.substr.php是您将获得的最全面的答案。
不过,如果您想以这种方式拆分字符串,我建议您使用explode
( http://www.php.net/manual/en/function.explode.php ) 函数。