preg-match
Matching a multiple lines pattern via PHP’s preg_match()
If you’re looking for (e.g.) a h2 tag nested within a td tag where there’s only whitespace in between the two, just use \s which includes spaces, newlines, etc. eg:: preg_match(‘#<td>\s*<h2>(.*?)</h2>\s*</td>#i’,$str,$matches); // result is in $matches[1] See it in action here. For your interest, here is a list of different modifiers you can pass in … Read more
preg_replace how to replace only matching xxx($1)yyy pattern inside the selector
Use backreferences (i.e. brackets) to keep only the parts of the expression that you want to remember. You can recall the contents in the replacement string by using $1, $2, etc.: preg_replace(‘/(text1)text2(text3)/is’,’$1$2′,$html);
PHP is_numeric or preg_match 0-9 validation
is_numeric() tests whether a value is a number. It doesn’t necessarily have to be an integer though – it could a decimal number or a number in scientific notation. The preg_match() example you’ve given only checks that a value contains the digits zero to nine; any number of them, and in any sequence. Note that … Read more
Regex to match an IP address [closed]
Don’t use a regex when you don’t need to 🙂 $valid = filter_var($string, FILTER_VALIDATE_IP); Though if you really do want a regex… $valid = preg_match(‘/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/’, $string); The regex however will only validate the format, the max for any octet is the max for an unsigned byte, or 255. This is why IPv6 is necessary – … Read more