Tricks and code snippets

Validation

IP

Before writing your own validators making extensive use of RegEx – check filter_var() and relatives (*_list, *_var_array, etc):

filter for IPv4 address
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
	echo "valid IPv4 address";
}

Caveat: FILTER_VALIDATE_MAC added in PHP 5.5.0 – can't be used in our project

 

Error Messages

Error messages for validation rules are specified in resources/lang/en/validation.php in Laravel 5.

Extracting numbers from string using RegEx in PHP

e.g. to extract or change „511“ from the original string "some foo:511-bar":

example script: backreferences.php