josef.van.niekerk
josef.van.niekerk

Reputation: 12121

Strings appear exact, but they do not match?

I have two strings that I am comparing in a unit test. When I echo these strings, they look exactly the same...but...when comparing them with strcmp, they are not.

The one string is slightly, about 3 characters, longer than the other when looking at strlen().

I suspect that this could be because of line feeds/newlines.

The on string, $a is loaded from a file (on Windows), and the other $b is generated by the script. I'm comparing $b to $a in a unit test.

How do I go about converting all line feeds so that they are the same, regardless?

Upvotes: 2

Views: 3347

Answers (1)

Dan Grossman
Dan Grossman

Reputation: 52372

First, find out if that's actually the problem. var_dump both strings in some environment where you'd see the blank lines if they're there.

Then, if line endings are the problem, remove them with trim

$str = trim($str);

Or str_replace

$str = str_replace(array("\r", "\n"), "", $str);

Upvotes: 4

Related Questions