function find_replace($find, $replace, $file, $case_insensitive = true)
{
	if(!file_exists($file))
	{
		return false;
	}
	else
	{
		$contents = file_get_contents($file);
		if($case_insensitive)
		{
			$output = str_ireplace($find, $replace, $contents);
		}
		else
		{
			$output = str_replace($find, $replace, $contents);
		}
		
		$fopen = fopen($file, 'w');
		if(!$fopen)
		{
			return false;
		}
		else
		{
			$fwrite = fwrite($fopen, $output);
			if(!$fwrite)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		fclose($open);
	}
}
4
Contributors
5
Replies
56
Views
5 Years
Discussion Span
Last Post by Echo89
1

not tested, but theoretically should work

This alone made me want to hit the vote down button, but I refrained ;) It's an acceptable start.
"In theory, theory and practice are the same. In practice, they’re not."

Room for improvements (IMHO):

  • Returning false is a little weird, you still do not know what's wrong. An exception would provide more information.
  • Why do you use file_get_contents but not file_put_contents ?
  • Returning the number of replacements is more useful than just true.
  • Regular expression search/replace would be useful.

Edited by pritaeas

Votes + Comments
file_put_contents() ... he missed it :)
0
<?php
/**
* Replace stuff in a file.
* 
* @throws InvalidArgumentException if $file is not a proper string
* @param path $file
* @param string $find
* @param string $replace
* @param bool $case_insensitive
* @return bool
*/
function find_replace($file, $find, $replace, $case_insensitive = true){
    // Validate arguments
    if(!is_string($file) or !strlen($file = trim($file))){
        throw new InvalidArgumentException('$file must be a non-empty trimmed string.');
    }

    // File not found
    if(!file_exists($file)) return false;

    // Empty so... no work here (but still a success)
    if(!filesize($file)) return true;

    // Fetch file contents
    $contents = file_get_contents($file);

    // Replacements go here
    $output = call_user_func_array($case_insensitive ? 'str_ireplace' : 'str_replace', $find, $replace, $contents);

    // Did we succeed?
    return (bool)file_put_contents($file, $output, LOCK_EX);
}

/**
* Case-insensitive alias of find_replace().
* 
* @param path $file
* @param string $find
* @param string $replace
* @return bool
*/
function find_ireplace($file, $find, $replace){
    return find_replace($file, $find, $replace, false);
}
?>

A rewrite of this slightly useless snippet. Shorter and more to the point.

Not to mention that the OP leaves the handle of the file open with his imbricated if else madness.

Once you return, you're out. No need to keep elsing after a return. Argh...

1

@CodeAngry...

The arrogance emanating from your post is suffocating.

To be honest I think your name matches your posts content.

Do me and everyone else a favour, shut up. If you don't like what I posted, don't be so negative about it. Simply point out my mistakes, and recommend ways of improving them like pritaeas has done (And that means not being so arrogant).

Besides, this was six months ago. I've learned a lot in that time. I've moved on to more serious projects.

Votes + Comments
I'm not arrogant... just to the point.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.