In PHP, you can extract a substring from a string using the `substr()` function. The `substr()` function takes three parameters: the string from which to extract the substring, the starting position (index) from where to begin extraction (0-based), and optionally the length of the substring to extract. If the length parameter is omitted, the substring will extend to the end of the string.
Here’s an example:
<?php $string = "Hello, World!"; $substring = substr($string, 7, 5); // Starting from index 7 (inclusive) and extracting 5 characters echo $substring; // Output: World ?>
In this example, `”World”` is extracted from the original string `”Hello, World!”` starting from index 7 (inclusive) and extracting 5 characters.
If you omit the length parameter, it will extract characters from the starting index to the end of the string:
<?php $string = "Hello, World!"; $substring = substr($string, 7); // Starting from index 7 (inclusive) to the end of the string echo $substring; // Output: World! ?>
In this case, it extracts `”World!”` from the original string.