Regular expression verification in PHP is a common concern of webmasters everywhere. If someone enters the form information, you need to verify it! But what is the simplest way to do this? Well, when dealing with a large number of text patterns that need to be followed, I always turn to regular expressions. Regular expressions are a way for you to define a pattern. & Nbsp; although I won't teach you Regex, I will tell the regular expression in PHP. PHP mainly uses the Perl regex taste. This is why the function to be executed is verified using a regular expression in PHP.
Verification is a common concern of webmasters everywhere. If someone enters the form information, you need to verify it! But what is the simplest way to do this?
Well, when dealing with a large number of text patterns that need to be followed, I always turn to regular expressions. Regular expressions are a way for you to define a pattern. Although I won't teach you Regex, I will tell the regular expression in PHP. PHP mainly uses the Perl regex taste. This is why the function to be executed is in the PHP regular expression preg_match. How can I perform regular expression matching in Perl? In any case, there are some modifiers that can be added to the PHP pattern and affect how it is executed. For example, "I" makes it case insensitive. I have to do this, email address mode, because it shortens the readability of assuming you use case-sensitive regular expression mode. Then, there is a "meter" modifier that will treat a multi-line subject and actually Multiple lining. Otherwise it will be ignored. the linefeed (N) and anchor (^ and $) will assume that you mean the beginning and end of the entire topic. "S" is useful. if you often use regular expression patterns, there are no consistent starting characters starting from ^. To confirm, if the user name and password are correct, I thought only letters and numbers are allowed. Therefore, searching for all characters that may be a potential error, rather than searching for characters that are not letters. This is made up of characters ^ [: ^ A-ZA-Z0-9] in character classes. Then, if there is a match, I know that there is an invalid character in the field. Remember that there are metacharacters that need to be escaped in the regular expression. I recommend that preg_quote email and URL values be relatively difficult, especially because these standards are difficult to define. For example, in the regular expressions.info, a fairly lengthy debate has reached the e-mail regex. Technically, a valid email address can contain single quotes. For most email addresses, the list will be valid. The URL contains similar issues. A large number of top-level domain names and a list are difficult to aggregate. There is also a tendency to replace "software8.co" http://software8.co ". There are also situations where top-level domain names contain the value of a country, and, like google. co. uk, I do not recommend trying, including as many as possible, you can. If you try to satisfy everyone, it will no longer be feasible if it takes so much time for your final verification. Verify user input, what is the right option in your code, rather than based on what they will enter. Besides, regex is not the solution to every problem (although it is for the majority ). If you want to verify something that has many exceptions or is extremely complex, it becomes a chore. I cannot imagine trying to format the date and time. 12 noon or midnight? Is the value 12: 60 valid? How about? Do you need input in the morning and afternoon? 3 means the same as 3: 00? These things are best left to humans or some other methods that you can develop.
$unit) { switch ($idx) { case 4:$rgx = "/^((ftp|http|https)://(w+:{0,1}w*@)?)?(S+)(:[0-9]+)?(/|/([w#!:.?+=&%@!-/]))?$/"; break; case 3: $rgx = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i"; break; case 2: //read below $rgx = "/[^A-Za-z0-9]+/"; break; case 1: //read below $rgx = "/[^A-Za-z0-9]+/"; break; case 0: //if characters are NOT normal $rgx = "/[^A-Za-z0-9]+/"; break; default: echo "???"; die("$unit ?"); break; } $n = preg_match($rgx, $unit, $matches); if ( ($idx == 0) || ($idx == 1) || ($idx == 2) ){ if ($n) { echo "Bad Characters in $unit; Alphanumeric only
"; } } else { if ($n == 0) { echo "Incorrect Format in $unit; Enter Valid Info
"; } }}?>