有用的正则表达式函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有用的正则表达式函数相关的知识,希望对你有一定的参考价值。
<?php //Some regex functions: //A better solution for validate email syntax is using filter_var. echo "Your email is ok."; } else { echo "Wrong email address format."; } //Validate username, consist of alpha-numeric (a-z, A-Z, 0-9), underscores, and has minimum 5 character and maximum 20 character. //You could change the minimum character and maximum character to any number you like. $username = "user_name12"; echo "Your username is ok."; } else { echo "Wrong username format."; } //Validate domain $url = "http://komunitasweb.com/"; if (preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i', $url)) { echo "Your url is ok."; } else { echo "Wrong url."; } //Extract domain name from certain URL $host = $matches[1]; echo $host; //Highlight a word in the content $text = "Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor"; echo $text; ?>
以上是关于有用的正则表达式函数的主要内容,如果未能解决你的问题,请参考以下文章