Home > Backend Development > PHP Tutorial > How to use PHP regular expressions to verify whether the input string is in the correct WeChat public account format

How to use PHP regular expressions to verify whether the input string is in the correct WeChat public account format

PHPz
Release: 2023-06-24 14:30:01
Original
1643 people have browsed it

As the number of users of WeChat official accounts continues to grow, more and more companies and individuals are beginning to use WeChat official accounts for promotion and marketing. In order to ensure the authenticity and legality of the official account, WeChat needs to review the name and account number of the official account. Therefore, for developers, how to use PHP's regular expressions to verify whether the input string is in the correct WeChat official account format has become a necessary skill.

The naming rules for WeChat public accounts are: both Chinese and English are acceptable, but pure numbers, special characters and emoticons are not supported, and the length must be within 30 characters. In addition, the account format of the WeChat official account consists of two parts, namely a combination of letters and numbers, with a length between 6-20 characters. Therefore, we can use PHP regular expressions to determine whether the input string conforms to the naming rules and account format of WeChat official accounts.

The following is a code example to introduce how to use PHP regular expressions to verify whether the input string is in the correct WeChat public account format.

<?php
function checkWeChatAccount($str) {
    //验证公众号名称和账号格式的正则表达式
    $nameRegExp = "/^[x{4e00}-x{9fa5}a-zA-Z]{1,30}$/u"; //支持中英文,长度在30个字符以内
    $accountRegExp = "/^[a-zA-Zd_]{6,20}$/"; //仅支持字母和数字的组合,长度在6-20个字符之间
    
    //先验证公众号名称是否符合格式
    if (!preg_match($nameRegExp, $str)) {
        return false;
    }
    
    //再验证账号格式是否符合要求
    if (!preg_match($accountRegExp, $str)) {
        return false;
    }
    
    //如果都符合要求,则返回true
    return true;
}

//测试代码
$str1 = "我的微信公众号"; //符合命名规则,但不符合账号格式
$str2 = "mywechat123"; //符合命名规则和账号格式
$str3 = "1234567890"; //不符合命名规则和账号格式
$str4 = "我的微信公众号123456"; //长度超出限制,不符合命名规则和账号格式

var_dump(checkWeChatAccount($str1)); //预期输出:false
var_dump(checkWeChatAccount($str2)); //预期输出:true
var_dump(checkWeChatAccount($str3)); //预期输出:false
var_dump(checkWeChatAccount($str4)); //预期输出:false
?>
Copy after login

Through the above code example, we can see that the checkWeChatAccount() function can verify whether the string meets the naming rules and account format requirements of WeChat public accounts, and returns true or false. When developing applications related to WeChat public accounts, by using PHP regular expressions, the readability and maintainability of the code can be effectively improved, thereby making the application more stable and secure.

The above is the detailed content of How to use PHP regular expressions to verify whether the input string is in the correct WeChat public account format. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template