How does preg_match_all() differ from preg_match() in terms of extracting multiple occurrences of a pattern in PHP?
preg_match_all() differs from preg_match() in that preg_match_all() will return all occurrences of a pattern in a string, while preg_match() will only return the first occurrence. This is useful when you need to extract multiple instances of a pattern from a string. Here is an example of how to use preg_match_all() to extract multiple occurrences of a pattern in PHP:
```php
$string = "The quick brown fox jumps over the lazy dog";
$pattern = '/\b\w{4}\b/'; // Match 4-letter words
preg_match_all($pattern, $string, $matches);
print_r($matches[0]);
```
In this example, preg_match_all() is used to find all 4-letter words in the given string and store them in the $matches array. The resulting array will contain all the matched words.
Related Questions
- Are there any common pitfalls or errors that can occur when working with image manipulation functions in PHP?
- What are the advantages and disadvantages of storing user input values as constants in a PHP script for a user interface?
- How can you securely synchronize a MySQL database without exposing the password in the source code?