|
Post by moneyman18 on May 27, 2011 20:32:52 GMT
I need to compare multiline strings. One string is user inputted data and the other is contained within a file located on the server. I thought it would be a piece of cake but turns out it doesn't seem to be that easy. I was using file_get_contents() to get the contents of the file and then comparing them using a basic if statement. It turns out that the two strings were not the same even though I copied them. I assume this has to do with the carriage return or line feed characters being in one and not the other. What I would like to know is how would I go about checking if they are the same even though they may or may not have carriage returns/line feeds? Any help would be much appreciated.
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on May 27, 2011 22:35:50 GMT
|
|
xcessive
Epic Poster
.[M:5000]
Posts: 526
|
Post by xcessive on May 28, 2011 3:43:34 GMT
You could just explode it using the newline character. $pieces = explode("\n", $string);
|
|
|
Post by moneyman18 on May 30, 2011 22:18:51 GMT
Not sure that would've worked. You could just explode it using the newline character. $pieces = explode("\n", $string); How would that have help me if one of the strings contained a carriage return and the other one didn't? It would just end up being an array with 1 element if it didn't contain a new line character or each array element except the last would have \r at the end of it, and if the other string didn't have the same carriage returns/new lines the arrays would still be different, right?
I ended up using preg_split('/(?:\r\n|[\r\n])/', $string) on both strings and compared the returned arrays. If there is a better way to do it without regex then please feel free to share as I could not find a better way of doing it.
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on May 31, 2011 23:53:25 GMT
Trim() should work if there is any white space at the beginning or end of the string that you want to remove which should include newlines and carriage returns.
|
|
|
Post by moneyman18 on Jun 1, 2011 2:11:18 GMT
Trim() should work if there is any white space at the beginning or end of the string that you want to remove which should include newlines and carriage returns. Yes, true. Maybe I am not making myself clear enough. I am not really worried about what is at the beginning or end of the string but focusing on the characters at the end of each line. Say the string from the file 'something.txt' is: Hello\n World! and the user inputted string is: Hello\r\n World! I would want it to return true when comparing them as both are the same with exception of the new line characters. When comparing them, what I would like is that everything in both of the strings to be the same (i.e. same text, amount of lines, ect) but for it not to matter what new line characters are used to separate the lines.
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on Jun 1, 2011 3:26:36 GMT
Ah, I see what you mean now. Yeah, I think the regular expression is the best way to go on this one.
|
|
|
Post by moneyman18 on Jun 7, 2011 3:44:43 GMT
Yea that is what I thought I was going to have to do.
|
|