PHP Regular Expression functions

Source: Internet
Author: User

  

PHP Regular Expressions

<?php
//Common Regular Expression function summary (all start with preg_, except Preg_quote, the first parameter is regular expression)
///1.preg_match ($pattern, $subject, $arr)//Match Single Form validation
//2.preg_match_all ($pattern, $subject, $arr)//Match multiple forms validation
///3.preg_filter ($pattern, $replacement, $subject) preserves only the string that has been replaced
4.preg_replace ($pattern, $replacement, $subject) all strings are displayed illegal word filtering
///5.preg_grep ($pattern, $subject) match only, do not replace
///6.preg_split ($pattern, $subject) Split string action is similar to explode function
///7.preg_quote ($STR); regular arithmetic escape function
Header(' content-type:text/html;charset= ' Utf-8 "');
functionShow ($arr= NULL)
{
if($arr== NULL)
{
Echo' null ';
}
ElseIf(Is_array($arr))
{
Echo' <pre> ';
Print_r($arr);
Echo' </pre> ';
}Else
{
Echo$arr;
}
}

/**
* PHP Regular expression function
* Preg_match ($parteen, $str, $arr); match a single
* Preg_match_all ($parteen, $str, $arr); match all
* Returns the number of matches to
*/
$parteen= '/[0-3]/';
$str= ' Weeffdsfsdfd3454fdxvdfg6vxv8 ';
$replacement= ' Test ';
/* $m 1 = $m 2 = array ();
$t 1 = preg_match ($parteen, $str, $m 1);
$t 2 = Preg_match_all ($parteen, $str, $m 2);
Show ($m 1);
echo '

Show ($m 2);
echo '

Echo Show ($t 1. ' | | '. $t 2); * *

/**
* Preg_replace and Preg_filter
* Preg_repace ($pattern, $replacement, $subject) will display all the strings
* Preg_filter ($pattern, $replacement, $subject) preserves only the strings that have been replaced
*/
/* $parteen = Array ('/[0123]/', '/[456]/', ' [789] ');
$str = Array (' Wewe ', ' w234 ', ' f454 ');
$replacement = Array (' High ', ' short ', ' fat ');
$t 1 = preg_replace ($parteen, $replacement, $STR);
$t 2 = Preg_filter ($parteen, $replacement, $STR);
Show ($t 1);
echo '

Show ($t 2);
echo '


/**
* PREG_GREP ($pattern, $subject) only do matches, do not replace
*/
/ * $parteen = '/[0-9]/';
$str = Array (' Wewe ', ' w234 ', ' f454 ');
$t 1 = preg_grep ($parteen, $STR);
Show ($t 1); * *

/**
* Preg_split ($pattern, $subject) Split string action is similar to explode function
*/
/ * $parteen = '/[0-9]/';
$subject = ' This 4 is 11 things to test ';
$t 1 = preg_split ($parteen, $subject);
Show ($t 1); * *

/**
* Preg_quote ($STR); regular arithmetic turn
*/
$str= ' Qwer[123]{sadas} ';
$t 1= Preg_quote($str);
Show ($t 1);
?>

PHP Regular Expression functions

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.