What is the best way to extract specific parts of a string in PHP?

When you need to extract specific parts of a string in PHP, you can use functions like `substr`, `strpos`, `preg_match`, or `explode` depending on the specific requirements. These functions allow you to extract substrings based on positions, patterns, or delimiters within the string. By utilizing these functions, you can easily extract the desired parts of a string efficiently.

// Example: Extracting a substring based on position using substr
$string = "Hello, World!";
$substring = substr($string, 7, 5); // Extracts "World"
echo $substring;

// Example: Extracting a substring based on a pattern using preg_match
$string = "The quick brown fox jumps over the lazy dog";
preg_match('/quick (.*?) fox/', $string, $matches);
echo $matches[1]; // Extracts "brown"

// Example: Extracting parts of a string based on a delimiter using explode
$string = "apple,orange,banana,grape";
$fruits = explode(",", $string);
echo $fruits[2]; // Extracts "banana"