public static function searchAndReplace($search, $replace, $pathAndFilename, $regularExpression = false)
    {
        $pathInfo = pathinfo($pathAndFilename);
        if (!isset($pathInfo['filename']) || $pathAndFilename === __FILE__) {
            return false;
        }

        $file = file_get_contents($pathAndFilename);
        $fileBackup = $file;
        if ($regularExpression === true) {
            if ($replace instanceof \Closure) {
                $file = preg_replace_callback($search, $replace, $file);
            } else {
                $file = preg_replace($search, $replace, $file);
            }
        } else {
            $file = str_replace($search, $replace, $file);
        }
        if ($file !== $fileBackup) {
            file_put_contents($pathAndFilename, $file);
            return true;
        }
        return null;
    }
public static function searchAndReplace($search, $replace, $pathAndFilename, $regularExpression = false)
    {
        $pathInfo = pathinfo($pathAndFilename);
        if (!isset($pathInfo['filename']) || $pathAndFilename === __FILE__) {
            return false;
        }

        $file = file_get_contents($pathAndFilename);
        $fileBackup = $file;
        if ($regularExpression === true) {
            if ($replace instanceof \Closure) {
                $file = preg_replace_callback($search, $replace, $file);
            } else {
                $file = preg_replace($search, $replace, $file);
            }
        } else {
            $file = str_replace($search, $replace, $file);
        }
        if ($file !== $fileBackup) {
            file_put_contents($pathAndFilename, $file);
            return true;
        }
        return null;
    }
public static function searchAndReplace ( $ search , $ replace , $ pathAndFilename , $ regularExpression = false ) { $ pathInfo = pathinfo ( $ pathAndFilename ) ; if ( ! isset ( $ pathInfo [ 'filename' ] ) || $ pathAndFilename === __FILE__ ) { return false ; } $ file = file_get_contents ( $ pathAndFilename ) ; $ fileBackup = $ file ; if ( $ regularExpression === true ) { if ( $ replace instanceof \ Closure ) { $ file = preg_replace_callback ( $ search , $ replace , $ file ) ; } else { $ file = preg_replace ( $ search , $ replace , $ file ) ; } } else { $ file = str_replace ( $ search , $ replace , $ file ) ; } if ( $ file !== $ fileBackup ) { file_put_contents ( $ pathAndFilename , $ file ) ; return true ; } return null ; }
Does a search and replace operation on the given file.

A simple str_replace is used, unless $regularExpression is set
to true. In that case preg_replace is used. The given patterns
are used as given, no quoting is applied!

In case $regularExpression is true, a closure can be given for
the $replace variable. It should return a string and is given an
array of matches as parameter.

@param string $search
@param string|\Closure $replace
@param string $pathAndFilename
@param boolean $regularExpression
@return boolean|NULL false on errors, NULL on skip, true on success
Does a search and replace operation on the given file .