Phppreg_filter: execute a regular expression to search for and replace _ PHP Tutorial

Source: Internet
Author: User
Phppreg_filter performs a regular expression search and replacement. Preg_filter (PHP55.3.0) preg_filter-execute a regular expression to search for and replace mixedpreg_filter (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit-1 [, preg_filter
(PHP 5> = 5.3.0)

Preg_filter-execute a regular expression to search and replace
Mixed preg_filter (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])
Preg_filter () is equivalent to preg_replace () except that it only returns (possibly converted) results that match the target. For more information about how this function works, see preg_replace.

Return value
If the subject is an array, an array is returned. Otherwise, a string is returned.

If no match is found or an error occurs, an empty array is returned when the subject is an array. otherwise, NULL is returned.

Example

Example #1 compare preg_filter () and preg_replace ()

The code is as follows:


$ Subject = array ('1', 'A', '2', 'B', '3', 'A', 'B', '4 ');
$ Pattern = array ('/\ d/', '/[a-z]/', '/[1a]/');
$ Replace = array ('A: $ 0', 'B: $ 0', 'C: $0 ');

Echo "preg_filter returns \ n ";
Print_r (preg_filter ($ pattern, $ replace, $ subject ));

Echo "preg_replace returns \ n ";
Print_r (preg_replace ($ pattern, $ replace, $ subject ));
?>


The above routine will output:

The code is as follows:


Preg_filter returns
Array
(
[0] => A: C: 1
[1] => B: C:
[2] => A: 2
[3] => B: B
[4] => A: 3
[7] => A: 4
)
Preg_replace returns
Array
(
[0] => A: C: 1
[1] => B: C:
[2] => A: 2
[3] => B: B
[4] => A: 3
[5] =>
[6] => B
[7] => A: 4
)


  • PCRE Patterns
  • Preg_replace ()-execute a regular expression to search and replace
  • Preg_replace_callback ()-execute a regular expression search and use a callback to replace
  • Preg_grep ()-returns array entries in matching mode
  • Preg_last_error ()-returns the error code generated by the last PCRE regular execution.

Http://www.bkjia.com/PHPjc/325093.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325093.htmlTechArticlepreg_filter (PHP 5 = 5.3.0) preg_filter-execute a regular expression to search and replace mixed preg_filter (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [,...

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.