Thursday 2 October 2014

Perform a regular expression search and replace

Perform a regular expression search and replace

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


Perform a regular expression search and replace. Its nice way to replace the text in single php command.


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.

Following are an Example of preg_filter and preg_replace.
<?php 
  if (!function_exists('preg_filter')) { 
    function preg_filter($pattern, $replace, $subject, $limit = -1 , &$count = null) { 
      if(!is_array($subject)) { 
        $noArray = 1 ; 
        $subject = array($subject); 
      } 
      $preg = preg_replace($pattern, $replace, $subject, $limit,  &$count); 
      $diff = array_diff($preg, $subject); 
      if($noArray == 1) $diff = implode($diff) ; 
      return $diff ; 
    } 
  } 
?>


Question: From where I can test regex online?
http://www.phpliveregex.com/