php - 复制字符串的一部分

Max*_*rai 0 php string

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

$string = 'one two three';
$numSymbols = 7;
 %what should I do%
$res_string = 'one two';
Run Code Online (Sandbox Code Playgroud)

请帮忙.

Pas*_*TIN 5

我想说你正在寻找substr函数.

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

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

$res_string = substr($string, 0, $numSymbols);
var_dump($res_string);
Run Code Online (Sandbox Code Playgroud)

你会得到:

string 'one two' (length=7)
Run Code Online (Sandbox Code Playgroud)

参数:

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