PHP Selection of Common Regular Expressions (Recommended)

  • 2021-12-11 17:28:35
  • OfStack

Regular expressions are commonly used in PHP, as follows:


$regex = '[\u4e00-\u9fa5]'; // Regular Expressions Matching Chinese Characters 
$regex = '^[\u4E00-\u9FA5A-Za-z0-9]+$'; or $regex = '^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$'; // Chinese, English, numbers but excluding underscores and other symbols 
$regex = '^[a-zA-Z][a-zA-Z0-9_]{4,15}$'; // Is the account number legal ( Beginning with letters, allowing 5-16 Bytes, allowing alphanumeric underscores )
$regex = '[^\x00-\xff]'; // Matching double-byte characters ( Including Chinese characters )
$regex = '\n[\s| ]*\r'; // A regular expression that matches a blank line 
$regex = '/<(.*)>.*<\/\1>|<(.*) \/>/'; // Matching HTML Marked regular expression 
$regex = '(^\s*)|(\s*$)'; // A regular expression that matches the first and last spaces 
$regex = '/(\d+)\.(\d+)\.(\d+)\.(\d+)/g'; // Matching IP Regular expression of address 
$regex = '\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*'; // Matching Email Regular expression of address 
$regex = '^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$'; // Mobile phone number 
$regex = '^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$'; //18 Bit ID number 
$data = "***********";
if (preg_match($regex,$data)) {
  echo " Verification succeeded ";
} else {
  echo " What are you typing in? ";
}

ps: Here are some common regular expressions PHP version

String filtering All non-English characters and special symbols preserve only English characters, numbers, and spaces:


$string = "abcd23uo*&* (. String passing ";
preg_match_all("/[\w\s]/i", $string, $matches);
print_r(implode('', $matches[0]));

String filters all non-Chinese characters:


$string = "abcd23uo*&* (. String passing ";
preg_match_all("/([\x{4e00}-\x{9fa5}])/u", $string, $matches);
print_r(implode('', $matches[0]));

Summarize


Related articles: