What are the best practices for validating IP addresses in PHP, especially when they are embedded within a larger text?

When validating IP addresses in PHP, especially when they are embedded within a larger text, it's important to use regular expressions to ensure they are in the correct format. One way to do this is by using the `filter_var` function with the `FILTER_VALIDATE_IP` flag. This function will return the IP address if it is valid, or `false` if it is not.

$text = "This is an example text with an IP address 192.168.1.1 embedded within it.";
$matches = [];
preg_match_all("/\b(?:\d{1,3}\.){3}\d{1,3}\b/", $text, $matches);

foreach ($matches[0] as $ip) {
    if (filter_var($ip, FILTER_VALIDATE_IP)) {
        echo "Valid IP address found: $ip\n";
    } else {
        echo "Invalid IP address found: $ip\n";
    }
}