filter-mapping PHP Preg_filter performs a regular expression search and replace

Source: Internet
Author: User
Preg_filter
(PHP 5 >= 5.3.0)
preg_filter-perform a regular expression 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 simply returns (possibly transformed) the result that matches the target. For more detailed information on how this function works, please read the preg_replace () documentation.
return value
If subject is an array, returns an array, and returns a string in other cases.
If no match is found or an error occurs, an empty array is returned when subject is an array, and other conditions return NULL.
Example
Example #1 Compare examples of Preg_filter () and Preg_replace ()

Copy the Code code 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 routines will output:

Copy the Code code as follows:


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


    • PCRE Patterns
    • Preg_replace ()-Performs a search and replace of a regular expression
    • Preg_replace_callback ()-Performs a regular expression search and replaces it with a callback
    • Preg_grep ()-Returns an array entry for the matching pattern
    • Preg_last_error ()-Returns the error code generated by the last Pcre regular execution

The above describes the filter-mapping PHP preg_filter to perform a regular expression search and replace, including the filter-mapping aspect of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.