Current Location: Home> Function Categories> preg_filter

preg_filter

Perform regular expression search and replacement
Name:preg_filter
Category:Regular processing PCRE
Programming Language:php
One-line Description:Returns a string or array of replaced pattern matches, provided that a match is found.

Definition and usage

preg_filter() function returns a string or array of strings where the pattern match has been replaced by the replacement string.

If the input is an array, the function returns an array. If the input is a string, this function returns a string.

This function is similar to preg_replace() , but with one difference: when no content matching the pattern is found in the input string, the string will not be used for the return value. In this case, if the input is a string instead of an array, the function returns null.

The replacement string may contain a backward reference in the form of \n or n , where n is the index of the group in the pattern. In the returned string, instances of \n and n will be replaced by the substring that matches the group, or if \0 or $0 is used, they will be replaced by the entire expression.

Example

In the string list, enclose the numbers in brackets:

 <?php
$input = [  
  "It's 5 o'clock now" ,  
  "40 days" ,  
  "There are no numbers here" ,  
  "In 2000"  
] ;  
  
$result = preg_filter ( '/[0-9]+/' , '($0)' , $input ) ;  
print_r ( $result ) ;  
?>

Try it yourself

grammar

 preg_filter ( pattern , replacement , input , limit , count )

Parameter value

parameter describe
pattern Required. Contains regular expressions indicating what to search for.
replacement Required. Will replace the string that matches the pattern. It may contain backreferences.
input Required. The string or array of strings to perform replacements in it.
limit

Optional. Sets the limit on the number of replacements that can be made in each string.

The default is -1, indicating no limit.

count Optional. After the function is executed, this variable will contain a number indicating the number of replacements that have been executed.
Similar Functions
Popular Articles