PHP regular expressions (7 common examples) _ PHP Tutorial

Source: Internet
Author: User
PHP regular expressions (seven common examples ). PHP regular expression (7 common examples) 1. verify that using filer_var in E-mail is better than using regular expression matching if (filter_var (test + email @ ansoncheung, FILTER_VALIDATE_EMAIL )) {echoYouremaili PHP regular expression (seven common examples)

1. verify email

Filer_var is better than regular expression matching.

if (filter_var('test+email@ansoncheung', FILTER_VALIDATE_EMAIL)) {    echo "Your email is ok.";} else {    echo "Wrong email address format.";}

2. verify the user name (between 5-20 verified user names)

$username = "user_name12";if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) {    echo "Your username is ok.";} else {    echo "Wrong username format.";}

3. verify the IP address

$IP = "198.168.1.78";if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) {    echo "Your IP address is ok.";} else {    echo "Wrong IP address.";}

4. verify the ZIP code

$zipcode = "12345-5434"; if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) { echo "Your Zip code is ok."; } else { echo "Wrong Zip code."; }

5. verify the domain name

$url = "http://ansoncheung.tk/"; 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."; }

6. extract domain names from a specific URL

$url = "http://ansoncheung.tk/articles"; preg_match('@^(?:http://)?([^/]+)@i', $url, $matches); $host = $matches[1];echo $host;

7. highlight keywords in text

$text = "Sample sentence from AnsonCheung.tk, regular expression 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";$text = preg_replace("/\b(regex)\b/i", '\1', $text);echo $text;


Token (seven common examples) 1. verify that using filer_var in E-mail is better than regular expression matching if (filter_var (test + email @ ansoncheung, FILTER_VALIDATE_EMAIL )) {echo Your email I...

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.