How to use the substr function to extract a substring in PHP?
In PHP, you can use the substr() function to extract a substring. The function takes three parameters: the original string, the starting position, and the length to extract. Here is an example of using the substr() function to extract a substring:
$string = "Hello World";
// 截取前5个字符
$substring = substr($string, 0, 5);
echo $substring; // 输出 "Hello"
// 截取从第6个字符开始到结束的字符
$substring = substr($string, 5);
echo $substring; // 输出 " World"
In the example above, substr($string, 0, 5) means extracting the first 5 characters from the string, starting from the first character; whereas substr($string, 5) means starting from the sixth character and extracting until the end of the string.