How can you verify if a string has a valid IP format in PHP?
To verify if a string has a valid IP format in PHP, you can use the `filter_var` function with the `FILTER_VALIDATE_IP` filter option. This function will check if the string is a valid IP address in either IPv4 or IPv6 format.
$ip = "192.168.1.1";
if (filter_var($ip, FILTER_VALIDATE_IP)) {
echo "Valid IP address";
} else {
echo "Invalid IP address";
}
Keywords
Related Questions
- What are the best practices for organizing and structuring symbols like classes, methods, and constants within PHP namespaces?
- What are some best practices for handling image insertion in fpdf for PDF generation in PHP?
- What are the advantages of using OOP principles when working with PHP GD for text output?