How can PHP string functions be used to manipulate and extract data efficiently?
PHP string functions can be used to manipulate and extract data efficiently by utilizing functions like `strlen`, `substr`, `strpos`, `str_replace`, and `explode`. These functions allow you to easily manipulate strings by finding the length of a string, extracting a substring, finding a specific character or substring within a string, replacing parts of a string, and splitting a string into an array based on a delimiter.
// Example: Using PHP string functions to manipulate and extract data efficiently
$string = "Hello, World!";
$length = strlen($string); // Get the length of the string
$substring = substr($string, 7); // Extract a substring starting from index 7
$position = strpos($string, ","); // Find the position of the comma
$newString = str_replace("Hello", "Hi", $string); // Replace "Hello" with "Hi"
$words = explode(" ", $string); // Split the string into an array based on spaces
echo "Length: " . $length . "\n";
echo "Substring: " . $substring . "\n";
echo "Position of comma: " . $position . "\n";
echo "New string: " . $newString . "\n";
print_r($words);