PHP substr() Explained: Extract Strings

The substr() function is used to extract a portion of a string, with the basic syntax as follows:

substr(string $string, int $start, ?int $length = null) : string

Explanation of parameters:

  1. $string: The original string to be truncated.
  2. $start: Starting position, counted from 0.
  3. $length: optional parameter, specifies the length to be trimmed. If not specified, it trims to the end of the string.

Original sentence: 我不能理解这个数学问题。

Paraphrased sentence: I can’t comprehend this math problem.

$string = "Hello, World!";
$substring = substr($string, 7); // 截取从第7个字符到字符串末尾的部分
echo $substring;
// 输出: World!
$string = "Hello, World!";
$substring = substr($string, 0, 5); // 截取从第0个字符开始的5个字符
echo $substring;
// 输出: Hello
bannerAds