How can functions like strpos, strrpos, and sscanf be utilized to efficiently extract specific parts of a string in PHP?

To efficiently extract specific parts of a string in PHP, functions like strpos, strrpos, and sscanf can be utilized. These functions help in finding the position of a substring within a string or extracting formatted data from a string. By using these functions, we can easily locate and extract the desired information from a given string.

// Using strpos to find the position of a substring in a string
$string = "Hello, World!";
$pos = strpos($string, ",");
$extracted = substr($string, 0, $pos);
echo $extracted; // Output: Hello

// Using sscanf to extract formatted data from a string
$string = "Name: John, Age: 30, Location: USA";
sscanf($string, "Name: %s, Age: %d, Location: %s", $name, $age, $location);
echo $name; // Output: John
echo $age; // Output: 30
echo $location; // Output: USA