Search code examples
phppreg-replacepreg-filter

preg_replace() versus preg_filter()


What are the differences between preg_replace() and preg_filter()?

In which situations should I use one instead of the other?

I tried reading the documentation, but still don't quite understand what the differences are.


Solution

  • preg_filter() is identical to preg_replace() except it only returns the (possibly transformed) subjects where there was a match. For details about how this function works, read the preg_replace() documentation.

    from: here

    So if the signature is

    preg_filter ( mixed $pattern , mixed $replacement , 
                  mixed $subject [, int $limit = -1 [, int &$count ]] )
    

    it returns the $subject arguments "transformed" (all the match with regex pattern are substitute) into an array