1

What is the most elegant and efficient way of comparing a string against text file in PHP.

The flow:

I have a file on server var\user\rinchik\file\fileName\version0.txt.

user comes on the web-site, does his thing and gets $string with N characters length.

I need to check is this string is identical to the contents of var\user\rinchik\file\fileName\version0.txt. If it is then do nothing. If its not then save this string here: var\user\rinchik\file\fileName\version1.txt.

Should i just read the file version0.txt into another string then hash both of them and compare hashes?

Or there is some hidden magic method like: save new string as version1.txt and the run something like file_compare('version0.txt', 'version1.txt'); and it will do the trick?

3 Answers 3

3

You should read the file version0.txt into a string $string1 and check if $string == $string1 (I think it's not necessary to hash $string and $string1).

There is no build-in function in PHP which can compare the contents of two files.

Another way, you can use shell command diff. Such as exec('diff version0.txt version1.txt', $res, $rev) and check the return value if ($rev == 0) {...}.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes... i like the exec () thing but about hashing, i think hashing is required: echo strlen('AAJJ')."-".strlen('AJAJ'); gives you 4-4 but from the users stand poin it 2 different strings.
I mean you can compare two string directly in PHP. Such as 'AAJJ' == 'AJAJ'.
1

Since you know the length of the string, you can first check strlen($string) == filesize($filename). If they are different, you don't even need to check the contents.

If they are the same, then you can just do $string == file_get_contents($filename), or hash them with md5/md5_file as in Marc's answer, to verify the contents being identical.

Of course, if the method of generating the string means that the file will always be the same length (such as the result of a hash function) then there's no point in checking the file size.

4 Comments

strlen() is much slower than filesize() will it make sense to just safe string as a second file and compare 2 files with filesize()?
Not really, because when you factor in the time it takes to create and write the file, you might as well just use strlen. Just make sure you save it to a variable first, then compare that variable to the filesizes.
Hmm... i dont think that its gonna work in either way. echo strlen('AAJJ')."-".strlen('AJAJ'); gives you 4-4 but from the users stand poin it 2 different strings.
That's why you check the length first. If the lengths are different, then it doesn't matter what the content is. If they are the same, only then do you need to actually check the content (which is slower)
0

Quick and dirty method, without having to write the string to a file first:

if (md5($string) == md5_file('/path/to/your/file')) {
  ... file matches string ..
} else {
  ... not a match, create version1.txt
}

1 Comment

Won't work, the file will contain data not present in the string. $ echo 'test' > test.txt; php -r 'echo md5("test") . "\n" . md5_file("test.txt");' gives hashes 098f6bcd4621d373cade4e832627b4f6 and d8e8fca2dc0f896fd7cb4cb0031ba249

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.