PHP substr Function Guide

The substr function is used to return a portion of a string.

Grammar:

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

Parameters:

  1. $string: The string from which to extract a substring.
  2. $start: the position where extraction begins, if negative, counting starts from the right side.
  3. $length: Optional, the length to extract. Defaults to the end of the string if not specified.

Example: “I don’t understand what you mean.”

Paraphrased: “I’m not sure what you’re trying to say.”

$str = "Hello, World!";
echo substr($str, 0, 5); // 输出 "Hello"
echo substr($str, 7); // 输出 "World!"
echo substr($str, -6); // 输出 "World!"

Note: the start and length parameters both start counting from 0.

bannerAds