php - 复制字符串的一部分

时间:2009-09-13 09:05:23

标签: php string

我如何获得字符串的一部分(在符号中)。 例如:

$string = 'one two three';
$numSymbols = 7;
 %what should I do%
$res_string = 'one two';

请帮助。

3 个答案:

答案 0 :(得分:5)

您正在寻找substr功能,我会说。

在你的情况下,这是一个例子:

$string = 'one two three';
$numSymbols = 7;

$res_string = substr($string, 0, $numSymbols);
var_dump($res_string);

你会得到:

string 'one two' (length=7)

参数:

  • 要从
  • 中提取的字符串
  • 第一个角色的位置;即0,如果你想从字符串的开头
  • 开始
  • 您想要的字符数:7

答案 1 :(得分:2)

使用php方法substr

$string = 'one two three';
$numSymbols = 7;
$res_string = substr($string, 0, $numSymbols);

答案 2 :(得分:2)

您应该使用substr()

示例:

$string = 'one two three';
$res_string = substr($string, 0, 7);// $res_string == 'one two'