The regular expression in PHP is fulfilled

Source: Internet
Author: User
Tags perl regular expression php regular expression alphanumeric characters
Regular expression Validation in PHP

Verification is a common concern for webmasters everywhere. If someone enters the form of information, you need to verify it! But what is the simplest way to do this?

Well, when dealing with a lot 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 will not teach you the regex, I will tell the regular expression in PHP. PHP mainly uses the taste of Perl's regex. This is why the function to be executed is preg_match in the PHP regular expression. Do perl regular expression matching, how to do it? In any case, there are certain modifiers that can be added to the pattern in PHP, affecting how it executes. For example, "I" makes it case insensitive. I have to do this, because the e-mail address pattern is shortened as it assumes that you use a case-sensitive regular expression pattern for readability. Then, there are "M" modifiers that will treat a multi-line theme, actually multiple liners. Otherwise it will be ignored, newline (N) and anchor (^ and $) will assume you mean the beginning and end of the entire topic. The "S" is useful if you often use regular expression patterns, and do not start with a ^ without a consistent starting character. In order to determine if the username, password and authentication password are correct, I think only alphanumeric characters are allowed. So looking for everything that could be a potentially wrong character, rather than I'm not a character search for letters. This is by the character in the character class ^ [: ^ a-za-z0-9]. Then, later, if there was a match, I knew there was an invalid character in the field. Also keep in mind that there are meta characters that need to be escaped in regular expressions. My recommendation Preg_quote e-mail and URL values are difficult, especially because these standards are difficult to define. For example, in regular expressions.info, quite lengthy debates have been made to email the regex. Technically, a valid e-mail address can contain single quotes. For most e-mail addresses, the listing will be valid. The URL contains a similar problem. Numerous top-level domains, and a list is difficult to gather. People also have a tendency, just say "software8.co" instead of "http://software8.co". There are also cases where top-level domains contain the value of a country, as well, like "google.co.uk", I don't recommend trying, including as much as you can. If you try to meet everyone and you end up validating on spending so much time, it's no longer feasible. Verify user input in what your code is expedient, and not according to what they will enter. In addition, Regex is not the solution to every problem (though it is for most). If you want to validate something that has many exceptions or is extremely complex, it becomes a chore. I can't even imagine trying date and time format. 12 O'Clock noon or midnight? is 12:60 valid? How about 17:00? Do you need to enter in the morning and afternoon? 3 means the same as 3:00? These things are the bestLeft to humans or some other method that you can formulate.

  $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 is 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
"; }}}?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.