0

What I would like to achieve is to assert that two strings are equal ignoring formatting

        $expected =
            '<div class="input select">
                <label for="countries">User Country</label>
                <select name="countries" id="countries">
                    <option value="">Select a country</option>
                    <option value="ger">Germany</option>
                    <option value="fra">France</option>
                    <option value="rus" selected="selected">Russia</option>
                </select>
            </div>';
       $actual = '<div class="input select"><label for="countries">User Country</label><select name="countries" id="countries"><option value="">Select a country</option><option value="ger">Germany</option><option value="fra">France</option><option value="rus" selected="selected">Russia</option></select></div>';
       $this->assertTextEquals($expected, $actual);

So far I got to this:

    public function assertTextEquals($expected, $actual)
    {
        $actualNoLineBreaks = preg_replace("/\r|\n/", "", $actual);
        $actualNoLongSpaces = preg_replace('!\s+!', ' ', $actualNoLineBreaks);
        $expectedNoLineBreaks = preg_replace("/\r|\n/", "", $expected);
        $expectedNoLongSpaces = preg_replace('!\s+!', ' ', $expectedNoLineBreaks);
        TestCase::assertEquals($expectedNoLongSpaces, $actualNoLongSpaces);
    }

but the problem is that there are still spaces where linebreaks were, f.ex.: select"> <label.

So what would be the regex to strip line breaks and all following spaces (until the first non-space character)?

Of course I could strip all spaces from both strings but that would make error messages hard to read and I am looking for an elegant solution.

bancer
  • 7,475
  • 7
  • 39
  • 58
  • 1
    Related https://stackoverflow.com/q/46548564/2943403 , https://stackoverflow.com/q/45955993/2943403 , https://stackoverflow.com/q/32060992/2943403 , https://stackoverflow.com/q/55780443/2943403 , https://stackoverflow.com/q/6394416/2943403 – mickmackusa Oct 09 '21 at 21:53

2 Answers2

1

Check this:

(( *\n+|\A) *)

\A is to remove any space at start of the string.

demo

HFZ
  • 437
  • 2
  • 5
1

You can use

trim(preg_replace('~\h*\R\s*~', '', $expected))

See the PHP demo and the regex demo. Details:

  • \h* - zero or more horizontal whitespaces (tabs, spaces...)
  • \R - any line break char (sequence)
  • \s* - and any zero or more whitespace chars.

The trim() function removes leading/trailing whitespace.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    https://stackoverflow.com/a/6394462/2943403 ? https://stackoverflow.com/a/55780460/2943403 ? – mickmackusa Oct 09 '21 at 21:58
  • @mickmackusa The first is broader than this question, and the second one is a poorly asked question with confusing requirements (title and example input/expected result imply different solutions are necessary). Both contain less efficient solutions than mine (more backtracking needed). So, I leave it up to you. – Wiktor Stribiżew Oct 09 '21 at 22:13