Skip to content
PHP Logo

How can the substr function be used to extract a portion of a string in PHP?

To extract a portion of a string in PHP, you can use the substr function. This function takes a string, a starting index, and an optional length parameter, and returns the extracted portion of the string. By specifying the starting index and length, you can extract a substring from the original string.

php
$string = "Hello, World!";
$substring = substr($string, 7, 5);
echo $substring; // Output: World