What are some common methods for extracting specific words from a string in PHP?
When working with strings in PHP, it is common to need to extract specific words or substrings from a larger string. One common method to achieve this is by using PHP's built-in string functions like `explode()` or `substr()`. These functions allow you to split a string into an array of words or extract a specific substring based on certain criteria, such as a delimiter or position.
// Example 1: Using explode() to extract specific words based on delimiter
$string = "Hello, world! This is a sample string.";
$words = explode(" ", $string);
echo $words[1]; // Output: world!
// Example 2: Using substr() to extract a specific substring based on position
$string = "Hello, world!";
$word = substr($string, 6, 5);
echo $word; // Output: world